JSF + JasperReport:在浏览器中打开+下载文件

时间:2018-07-25 07:54:10

标签: java jsf jasper-reports downloadfile

我在jsf项目中使用jasper报告。我想在浏览器中打开文件然后下载。但我无法下载。这是我的代码:

public void doPdf(List<?> list, String pathTemplate, String fileName) throws JRException, IOException {
    String fileNameReport = new StringBuilder("filename=\"").append(fileName).append("\"").toString();
    
    JRBeanCollectionDataSource jr = new JRBeanCollectionDataSource(list);
    InputStream jasperTemplate = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(pathTemplate);
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperTemplate);
    
    Map<String, Object> parameters = new HashMap<>();
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jr);
    HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", fileNameReport);
     
    ServletOutputStream outputStream = response.getOutputStream();
    JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);

    outputStream.flush();
    outputStream.close();
    FacesContext.getCurrentInstance().responseComplete();
  }

0 个答案:

没有答案