如何使用 Java Springboot 返回 zip 文件流 [英] How to return a zip file stream using Java Springboot
问题描述
我用的是 Springboot,我想生成 zip 文件然后返回前端.
I use Springboot, I want to generate zip file and then return to frontend.
@PostMapping(value="/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<ZipOutputStream> export() {
// customService.generateZipStream() is a service method that can
//generate zip file using ZipOutputStream and then return this stream
ZipOutputStream zipOut = customService.generateZipStream();
return ResponseEntity
.ok()
.header("Content-Disposition", "attachment;filename=export.zip")
.header("Content-Type","application/octet-stream")
.body(zipOut)
}
可以正确生成 zip 文件(在本地目录中),但在将流返回到前端时出现以下错误:
The zip file can be generated correctly(in local dir) but I got below error when return stream to frontend:
spring.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
然后我检查了谷歌并将返回类型更改为ResponseEntity<StreamResponseBody>
,但是我应该如何在方法ZipOutputStream
到StreamResponseBody
code>body(...),google 中的解决方案是在 body()
方法中创建 zip 输出流,如下所示:
Then i checked in google and changed return type to ResponseEntity<StreamResponseBody>
, but how should I change ZipOutputStream
to StreamResponseBody
in method body(...)
, the solution in google is create zip output stream within body()
method like that:
// pseudocode
.body(out -> {
ZipOutputStream zipOut = new ZipOutputStream(out));
zipOut.putEntry(...);
zipOut.write(...);
zipOut.closeEntry();
... balabala
}
我的问题是如何在这种情况下使用 StreamResponseBody
或任何替代解决方案来返回可能有点大的 zip 流.
My question is how to use StreamResponseBody
in this scenario or any alternative solution to return a zip stream that might a little large.
推荐答案
感谢大家的帮助,我检查了你的答案并通过更新导出逻辑解决了这个问题.
Thanks for everyone's help, I checked yours answers and solved this issue by updating export logic.
我的解决方案:
将方法重新定义为 customService.generateZipStream(ZipOutputStream zipOut)
以便我可以在 controller 层和 StreamResponseBody
中创建一个压缩流然后将其发送到服务层,在服务层,我将进行导出.
Re-define method as customService.generateZipStream(ZipOutputStream zipOut)
so that I can create a zip stream with StreamResponseBody
in controller layer and then send it to service layer, in service layer, i will do export.
伪代码如下:
@PostMapping(value="/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity< StreamResponseBody > export() {
// customService.generateZipStream() is a service method that can
//generate zip file using ZipOutputStream and then return this stream
return ResponseEntity
.ok()
.header("Content-Disposition", "attachment;filename=export.zip")
.body(outputStream -> {
// Use inner implement and set StreamResponseBody to ZipOutputStream
try(ZipOutputStream zipOut = new ZipOutputStream(outputStream)) {
customService.generateZipStream(zipOut);
}
});
}
customService
前置代码:
public void generateZipStream(ZipOutputStream zipOut) {
// ... do export here
zipOut.putEntry(...);
zipOut.write(...);
zipOut.closeEntry();
// ... balabala
}
希望如果您有类似的问题,它可以帮助您.
Hope it can help you if you have similar question.
这篇关于如何使用 Java Springboot 返回 zip 文件流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!