【问题标题】:How to put two jasperReports in one zip file to download?如何将两个 jasperReports 放在一个 zip 文件中进行下载?
【发布时间】:2015-03-13 12:14:35
【问题描述】:
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 "";
}

这是我在 managedBean 中的操作方法,用户调用它来打印 JasperReport,但是当我尝试在 zip 文件中放入多个报告时,它不起作用。

getPeriodScales 正在返回两个对象,并且 JasperFillManager.fillReport 正在正确运行,因为当我只为一个报告生成数据时,当我尝试流式传输两个报告并在 WinRar 中打开时,只有一个出现并且我得到“未加急结束” of archive”,在 7zip 中都出现,但第二个已损坏。

我做错了什么,或者有没有办法在不压缩的情况下流式传输多个报告?

【问题讨论】:

    标签: jsf-2 jasper-reports outputstream zipoutputstream


    【解决方案1】:

    我知道是什么,我将响应的 contentLenght 设置为 bytes.length 大小,但它应该是 bytes.length * Scale.getPeriodScales(this.startDate, this.endDate).size()

    【讨论】:

      猜你喜欢
      • 2016-05-13
      • 2016-11-03
      • 2015-01-02
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 2020-09-15
      相关资源
      最近更新 更多