压缩多个文件导出


        List<Map<String, Object>> zipEntry = new LinkedList<>();

        for (File file : fileList) {
            zipEntry.add(getDocMap(file.getName(), file));

        }

        //压缩文件夹
        String pathname = "批量导出建议详情__" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";
        //File zipfile = new File(pathname);
//        response.setCharacterEncoding("UTF-8"); // 重点突出
        response.setContentType("application/zip");
        // 对文件名进行编码处理中文问题
//        pathname = new String(pathname.getBytes(), StandardCharsets.UTF_8);
        //设置前台下载压缩包名
        try {
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(pathname.getBytes("utf-8"), "ISO8859-1"));
        } catch (UnsupportedEncodingException e) {
            log.error("", e);
        }
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Location", pathname);

        byte[] buf = new byte[1024];
        try {
            ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
            for (int i = 0; i < zipEntry.size(); i++) {
                ZipEntry zipEntryDetail = (ZipEntry) zipEntry.get(i).get("zip");
                InputStream in = (InputStream) zipEntry.get(i).get("stream");
                // Add ZIP entry to output stream.
                out.putNextEntry(zipEntryDetail);
                // Transfer bytes from the file to the ZIP file
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                // Complete the entry
                out.closeEntry();
                in.close();
            }
            out.close();
        } catch (IOException e) {
            log.error("", e);
        } finally {
            try {
            //这个很有用,解决网址为 http://rental/anormaly 的网页可能暂时无法连接,或者它已永久性地移动到了新网址。
                response.flushBuffer();
            } catch (IOException e) {
                log.error("", e);
            }

            FileUtil.deleteFile(fileList.get(0).getParentFile());
        }
        

    /**
     * 获取一个doc文件 压缩对象
     *
     * @param fileName
     * @throws IOException
     */
    public Map<String, Object> getDocMap(String fileName, File file) {
        Map<String, Object> map = new HashMap<>(3);
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            ZipEntry zipEntry = new ZipEntry(fileName);
            map.put("stream", is);
            map.put("zip", zipEntry);
        } catch (FileNotFoundException e) {
            log.error("", e);
        }
        return map;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值