我正在使用此代码下载包含jasper报告的zip文件。如何在Servlet上使用更少的内存?我可以使用磁盘空间而不是内存吗?或者是否有任何其他方式,以便我的服务器不会给我一个错误说"内存不足"如果许多用户会尝试同时下载zip文件或者zip文件很大。
public void zipJasper(List<JasperPrint> print) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(baos);
try {
int k = 1;
for (int j = 0; j <= print.size(); ++j) {
byte[] buffer = new byte[1024];
byte[] pdfAsBytes = JasperExportManager.exportReportToPdf(print.get(j));
ByteArrayInputStream fis = new ByteArrayInputStream(pdfAsBytes);
zipfile.putNextEntry(new ZipEntry(print.get(j).getName() + ".pdf"));
int length;
while ((length = fis.read(buffer, 0, 1024)) > 0) {
zipfile.write(buffer, 0, length);
}
// zipfile.closeEntry();
// close the InputStream
fis.close();
}
// zipfile.close();
} catch (JRException ex) {
log("An error occured", ex);
} catch (IOException ex) {
log("An error occured", ex);
} finally {
try {
// Flush the stream
zipfile.flush();
} catch (Exception e) {
}
try {
// Close the stream
zipfile.close();
} catch (Exception e) {
}
}
}