我正在使用PrimeFaces 5.1和JSF 2.2
以下是我的代码:我从TableModelDate
方法生成public void PDF() throws JRException, IOException {
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
simpleReport();
httpServletResponse.addHeader("Content-disposition", "filename=report.pdf");
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint,servletOutputStream);
FacesContext.getCurrentInstance().responseComplete();
}
public void simpleReport() {
TableModelData();
try {
String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/reports/");
JasperCompileManager.compileReportToFile(reportPath+"/Report.jrxml");
jasperPrint = JasperFillManager.fillReport(reportPath+"/Report.jasper", new HashMap(),new JRTableModelDataSource(tableModel));
} catch (JRException ex) {
ex.printStackTrace();
}
}
文件一切正常。但它在我的浏览器中打开如何在不打开的情况下下载它。
{{1}}
答案 0 :(得分:2)
你非常接近它。
只需在attachment;
添加filename=xx.pdf
语句
像:
httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");
告诉Http Servlet Responce该文件包含附件;
<强>附加强>
每当浏览器打开PDF
格式时,它也会显示保存选项,您也可以在浏览器中打开(加载)后保存它