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中的action方法,用户调用它来打印JasperReport,但是当我尝试在zip文件中放入多个报表时,它无法正常工作。
getPeriodScales正在返回两个对象,并且JasperFillManager.fillReport正在正确运行,因为当我为一个报告生成数据时打印报告,当我尝试流式传输两个报告并在WinRar中打开时只出现一个,我得到一个&#34 ;存档的未删除结束",在7zip中都出现,但第二个已损坏。
我做错了什么或有没有办法在不压缩的情况下流式传输多个报告?
答案 0 :(得分:0)
我弄清楚是什么,我设置的响应内容长度为bytes.length
,但应该是bytes.length * Scale.getPeriodScales(this.startDate, this.endDate).size()