首页 > 解决方案 > 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);

    }

标签: javaspring-bootfiledownload

解决方案


推荐阅读