除了内容类型之外,您的代码不需要关心它是zip文件还是其他文件类型。它只需要传输文件。
特别是,不要使用
ZipFile.size()
对于
Content-Length
像
ZipFile.size()
返回zip文件中的文件数,而
内容长度
应为字节数。
你没有具体说明什么
httpResponse
是和从哪里来的。而且它看起来不像惯用的Spring Boot。更好的Spring Boot代码是:
@RequestMapping("/photoBatch")
public ResponseEntity<InputStreamResource> downloadPhotoBatch() throws IOException {
File file = new File("/Users/john/59c49360-3b55-4f97-910e-9f33907f10cb.zip");
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName())
.contentType(MediaType.parseMediaType("application/zip")
.contentLength(file.length())
.body(resource);
}