我使用Jasper Report
创建REST应用程序和PDF,并希望在浏览器上显示PDF文件下载对话框。如何在jax-rs中显示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); }
}
}
你是对的。我应该有文件实例不是文件路径。谢谢!第二个问题,我要检查.. –