在浏览器无效的情况下,Jasper报告生成的pdf下载选项

时间:2017-02-18 11:00:11

标签: java jasper-reports export-to-pdf

我试图使用jasper报告生成pdf。它的生成很好,它直接存储在指定的路径中。但我需要的是,它必须在直接下载之前显示打开或保存选项。我正在使用struts 1.x

这是我在方法

中编写的代码
String reportsPath = "D:/JasperReports/";
String reportName = "StatisticsReport";
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("statisticsData", map);

// Load JRXML

 JasperDesign mainReportDesign = JRXmlLoader.load(reportsPath + reportName + ".jrxml");
// Compile to generate .Jasper file
   JasperCompileManager.compileReportToFile(mainReportDesign, reportsPath + reportName + ".jasper");

   System.out.println(reportsPath + reportName);
                 // Generate Jasper print
   JasperPrint jasperPrint = JasperFillManager.fillReport(reportsPath + reportName + ".jasper", parameters,
                             new JREmptyDataSource());

   String pdfFileName = "D:/JasperReports/StatisticsrReport.pdf";
                 // Export PDF file
   response.addHeader("Content-disposition", "attachment; filename=StatisticsrReport1.pdf");
                 JasperExportManager.exportReportToPdfFile(jasperPrint,pdfFileName);

1 个答案:

答案 0 :(得分:1)

当响应内容类型为application/x-download时,浏览器将提示保存或打开文件。例如:

//String pdfFileName = "D:/JasperReports/StatisticsrReport.pdf";
response.setContentType("application/x-download");
response.addHeader("Content-disposition", "attachment; filename=StatisticsrReport1.pdf");
OutputStream out = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint,out);//export PDF directly
//return null ActionForward, no extra data is appended to output stream