我正在使用创建 REST 应用程序和 PDF,Jasper Report
并希望在浏览器上显示 PDF 的文件下载对话框。
这正是我正在寻找的:
http ://www.mkyong.com/webservices/jax-rs/download-pdf-file-from-jax-rs/
我下面的代码创建 PDF 文件 (MyAwesomeJasperReport25.pdf) 但文件下载对话框没有显示在浏览器上,我不明白为什么。
@GET
@Path("pdf")
@Produces("application/pdf")
public Response outputPDF() {
OutputStream output = null;
try {
File jrxmlFile = new File("C:\\Users\\m-takayashiki\\report2.jrxml");
if(jrxmlFile.exists()) {
//jrxml compile
JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlFile.getAbsolutePath());
//some code emitted
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
String filePath = "C:\\Users\\m-takayashiki\\MyAwesomeJasperReport25.pdf";
output = new FileOutputStream(new File(filePath));
JasperExportManager.exportReportToPdfStream(jasperPrint, output);
// From here trying to ask user to download PDF
ResponseBuilder response = Response.ok((Object) filePath);
response.header("Content-disposition",
"attachment; filename=MyAwesomeJasperReportDownload.pdf");
return response.build();
}
}
catch(Exception e) {
System.out.println("-------------------- PDF exception ");
System.out.println(e);
return null;
}
finally {
try {
if(output != null) { output.close(); }
}
catch(Exception e) { System.out.println(e); }
}
}