微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Boot FileLİst 下载 zip

如何解决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,8192)) != -1) {
                    out.write(buffer,bytesRead);
                }
                out.flush();
                out.close();
        
        return ResponseEntity.ok(HttpStatus.OK);

    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。