java 批量下载并压缩成一个zip文件

java 批量下载并压缩成一个zip文件,第1张

java 批量下载并压缩成一个zip文件
@RequestMapping("/zipDownload2")
    public void zipDownload2(HttpServletResponse resp) throws baseAppException {
        //要下载的文件
        List fileUrlList = new ArrayList<>();
        fileUrlList.add("D:\笔记文档\linux下搭建项目服务.txt");
        fileUrlList.add("C:\Users\Downloads\客户拜访报告模板.xlsx");

        resp.setHeader("Content-disposition", "attachment;filename=压缩包.zip");
        //最终zip文件
        try (ZipOutputStream zipOut=new ZipOutputStream(resp.getOutputStream())){
            // 循环调用压缩文件方法,将一个一个需要下载的文件打入压缩文件包
            for(String fileUrl : fileUrlList){
                InputStream inputStream = new FileInputStream(fileUrl);
                byte[] bytes = org.apache.poi.util.IOUtils.toByteArray(inputStream);
                zipOut.putNextEntry(new ZipEntry(FileUtils.getFileName(Utils.decodeUrl(fileUrl))));
                zipOut.write(bytes);
            }
        } catch (Exception e) {
            throw new baseAppException("zip download failed."+e.getMessage(), e);
        }
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5683325.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存