我发现我可以执行以下操作来解决我的问题 -
在我的 bean 类中,我编写了以下代码 -
HttpServletResponse response = (HttpServletResponse) getFacesContext().getExternalContext().getResponse();
ByteArrayOutputStream outputStream = null;
try {
outputStream = //Call service to generate the extract, return outputStream ;
if(outputStream != null){
response.setContentType(T3Constants.MIME_ZIP);
response.setHeader(T3Constants.CONTENT_DISPOSITION, ATTACH_FILE_NAME);
response.getOutputStream().write(outputStream.toByteArray());
response.flushBuffer();
FacesContext.getCurrentInstance().responseComplete();
outputStream.close();
} else {
formatFacesMessage(FacesMessage.SEVERITY_WARN, "No data for selected filter", "No data for selected filter");
}
}
在我的服务中,我编写了以下代码 -
ByteArrayOutputStream outputStream = null;
if(CollectionUtils.isNotEmpty(list returned from DB)) {
outputStream = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(outputStream);
//Add files to ZIP
outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
zos.putNextEntry(new ZipEntry(xlsName.toString()));
zos.write(outputStream.toByteArray());
zos.flush();
zos.close();
return outputStream;
}
谢谢,拉兹