我正在尝试将 JasperReports 生成的 PDF 文件发送到用户的浏览器,但在我的托管 bean 方法中找不到什么问题,这是一个片段:
System.out.println("Making pdf...");
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
String tplPath = ec.getRealPath("testTemplate.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(tplPath);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<String,Object>(), ds );
String pdfName = "/testReport.pdf";
String pdfPath = ec.getRealPath(pdfName);
JasperExportManager.exportReportToPdfFile(jasperPrint, pdfPath);
System.out.println("PDF ready!");
ec.responseReset();
ec.setResponseContentType(ec.getMimeType(pdfPath));
//ec.setResponseContentLength(contentLength);
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + pdfName + "\"");
InputStream input = new FileInputStream(pdfPath);
OutputStream output = ec.getResponseOutputStream();
IOUtils.copy(input, output);
System.out.println("Sending to browser...");
fc.responseComplete();
它由一个简单的用户点击调用:
<p:menuitem value="TestPDF" action="#{menuController.getTestPdf()}" />
我觉得这很容易找到。为什么我看不到?:)