java - Spring Boot FileLİst 下载 zip
问题描述
在spring boot项目中,我想将他用户的所有文件下载到一个zip中。
@GetMapping("/allFilePerson/{personId}")
public ResponseEntity<HttpStatus> allFilePerson(@PathVariable(value = "personId") Integer personId) {
List<file> fileList=service.findByFileId(personId);
....
}
此代码单个文件下载。但转换为fileList zip或rar。
@RequestMapping("/downloadFile/{fileId}")
public ResponseEntity<HttpStatus> handleFileDownloadPage(HttpServletRequest request, HttpServletResponse response, @PathVariable(value = "fileId") Integer fileId) throws IOException, Exception {
File file = service.getFile(fileId);
ServletOutputStream out = response.getOutputStream();
InputStream stream = null;
stream = new FileInputStream(file.getFilePath());
//write the file to the file specified
int bytesRead = 0;
byte[] buffer = new byte[8192];
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", String.format(" attachment; filename=\"%s\"", file.getFileName()));
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
out.close();
return ResponseEntity.ok(HttpStatus.OK);
}
解决方案
推荐阅读
- android - RecyclerView 不为空
- postgrest - PostgREST 查询的 curl 语法
- node.js - 带数字的票务通道名称
- javascript - 水平滚动div的内容被剪切
- javascript - fetch-API ReadableStream 是否与 promise.then(...) 一起使用?
- regex - 前 3 个字符的正则表达式应该类似于 SRU
- flutter - Flutter 的 OutlineButton 小部件中奇怪的黑色背景
- python - 当我使用 on_click 时如何检测触摸
- android - 每秒检查一次并调整 OnDesroy
- http - 如何清除 302 重定向上的片段标识符?