我正在从内存中下载zip文件(前端有一个按钮,单击该按钮,我将在后端生成一个zip文件并返回字节数组作为响应)。
这是我的控制者:
@ResponseBody
@PostMapping(value = "/documents/zip", produces = "application/zip")
public byte[] getDocumentsAsZip(@RequestBody List<Long> docIds, HttpServletResponse response) throws IOException, DocumentException, InvoiceException, JRException {
response.setStatus(HttpServletResponse.SC_OK);
response.addHeader("Content-Disposition", "attachment; filename=\"docs.zip\"");
return documentService.generateZip(docIds);
}
以下是构建zip本身的服务方法:
@Override
public byte[] generateZip(List<Long> docIds) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
for (Long docId : docIds) {
Document document = findDocumentById(docId);
String fileName = generateFileName(document);
byte[] pdfFile = pdfService.generatePdfFile(document);
ZipEntry entry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(entry);
zipOutputStream.write(pdfFile);
zipOutputStream.closeEntry();
}
zipOutputStream.close();
return outputStream.toByteArray();
}
现在,当我单击前端上的按钮时,我收到来自后端的200响应,并且在浏览器中我看到响应已生成,但是由于某种原因没有任何反应,因此没有进行下载。