我试图使用碧玉报告生成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);

最佳答案

响应内容类型为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

08-05 20:00
查看更多