浏览器中 Jasper 报告生成的 pdf 的下载选项不起作用
Download option for pdf generated by Jasper reports in browsers not working
我正在尝试使用 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);
当响应内容类型为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
我正在尝试使用 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);
当响应内容类型为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