0
我一直试图在使用ServletOutputStream的Web浏览器中显示PDF报告。到目前为止,我可以下载该文件,但无法将其显示在浏览器中。这里是我的代码如何使用ServletOutputStream在Web浏览器中显示PDF
private void initReport()
{
DBConnection connection = new DBConnection();
try {
jasperPrint = JasperFillManager.fillReport(getContext().getExternalContext().getRealPath("/WEB-INF/reports/testReport.jasper"), new HashMap(),connection.getConnection());
} catch (JRException ex) {
Logger.getLogger(ReportBacking.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void showPDF()
{
initReport();
try {
HttpServletResponse httpServletResponse;httpServletResponse = (HttpServletResponse) getContext().getExternalContext().getResponse();
httpServletResponse.setContentType("application/pdf");
httpServletResponse.addHeader("Content-disposition", "inline;filename=testReport.pdf");
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint,servletOutputStream);
getContext().responseComplete();
getContext().renderResponse();
} catch (JRException | IOException ex) {
Logger.getLogger(CertificateApplicationAddBacking.class.getName()).log(Level.SEVERE, null, ex);
getContext().addMessage(null, new FacesMessage("Could not generate report"));
}
如果我将content-diposition更改为附件。我可以下载文件。
我试过你的建议,它在firefox中完美工作,但在铬我得到这个错误:'资源解释为文档,但与MIME类型应用程序/ PDF传输' – unleashed