我正在使用此代码下载包含碧玉报告的 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) {
}
}
}