gpt4 book ai didi

java - Jasper 报告生成的 pdf 的下载选项在浏览器中不起作用

转载 作者:行者123 更新时间:2023-11-30 02:39:01 31 4
gpt4 key购买 nike

我正在尝试使用 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

关于java - Jasper 报告生成的 pdf 的下载选项在浏览器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42314267/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com