2015-03-13 110 views
0
public String generateReport() { 
try 

      { 

       final FacesContext facesContext = FacesContext.getCurrentInstance(); 
       final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); 
       response.reset(); 
       response.setHeader("Content-Disposition", "attachment; filename=\"" + "myReport.zip\";"); 
       final BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); 
       final ZipOutputStream zos = new ZipOutputStream(bos); 

       for (final PeriodScale periodScale : Scale.getPeriodScales(this.startDate, this.endDate)) 
       { 
        final JasperPrint jasperPrint = JasperFillManager.fillReport(
         this.reportsPath() + File.separator + "periodicScale.jasper", 
         this.parameters(this.reportsPath(), periodScale.getScale(), 
          periodScale.getStartDate(), periodScale.getEndDate()), 
         new JREmptyDataSource()); 

        final byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint); 
        response.setContentLength(bytes.length); 

        final ZipEntry ze = new ZipEntry("periodicScale"+ periodScale.getStartDate() + ".pdf"); // periodicScale13032015.pdf for example 
        zos.putNextEntry(ze); 
        zos.write(bytes, 0, bytes.length); 
        zos.closeEntry(); 
       } 
       zos.close(); 
       facesContext.responseComplete(); 
      } 
      catch (final Exception e) 
      { 
       e.printStackTrace(); 
      } 

      return ""; 
} 

這是我在被用戶調用來打印JasperReport的managedBean中的操作方法,但是當我嘗試在zip文件中放入多個報表時,它不起作用。如何把兩個jasperReports放在一個zip文件中下載?

getPeriodScales返回兩個對象,JasperFillManager.fillReport正常運行,因爲當我爲一個報告生成數據時,報告打印出來,當我嘗試流兩個報告時,只在WinRar中打開一個報告,而我得到一個「未加密檔案末尾「,在7zip中均出現,但第二個已損壞。

我在做什麼錯了或是否有一種方法來流多個報告而不壓縮它?

回答

0

我想通了什麼,我設置與bytes.length尺寸響應的contentLenght,但它應該是bytes.length * Scale.getPeriodScales(this.startDate, this.endDate).size()

相關問題