问题描述
在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,8192)) != -1) {
out.write(buffer,bytesRead);
}
out.flush();
out.close();
return ResponseEntity.ok(HttpStatus.OK);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)