我面临一个非常奇怪的问题,我试图使用下面的代码从我的struts应用程序发送PDF文件作为附件,
JasperReport jrReport = (JasperReport) JRLoader.loadObject(jasperReport);
JasperPrint jasperPrint = JasperFillManager.fillReport(jrReport, parameters, dataSource);
jasperPrint.setName(fileNameTobeGivenToExportedReport);
response.reset();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileNameTobeGivenToExportedReport + ".pdf" + "\"");
response.setHeader("Cache-Control", "private");
JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
但正在下载的PDF没有数据,意味着它显示空白页面.
在上面的代码中我添加了以下行以将PDF文件保存在我的D:目录中
File pdf = new File("D:\\sample22.pdf");
JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream(pdf));
生成的文件是正确的,意味着所有数据.有一件事我注意到从浏览器下载的文件和“sample22.pdf”具有相同的大小.
我读了一篇文章说这可能是服务器配置的问题,因为我们的服务器可能会破坏输出流.这是我读过的文章Creating PDF from Servlet.
这篇文章说
This can happen when your server flattens all bytes with a value higher than 127. Consult your web (or application) server manual to find out how to make sure binary data is sent correctly to the browser.
我正在使用struts 1.x,jBoss6,iReport 1.2