【发布时间】:2012-05-09 06:56:39
【问题描述】:
我正在使用 Jasper Report 创建 REST 应用程序和 PDF,并希望在浏览器上显示 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); }
}
}
【问题讨论】:
标签: java rest pdf download jax-rs