java 压缩文件

  今天改善一个项目,一个单据里面有十多个附件,其中一个需求是希望选中一个单据,点击导出按钮,将所有的附件都下载下来,

一开始考虑模拟浏览器窗口点击保存自动下载,但感觉实现有点复杂,也不太熟悉。所以就想了一种简单的方法,把所有附件进行压缩,

再下载压缩包。虽然多了用户解压缩的步骤,但总体还是方便很多。以下是实现过程:

1、导入jar包:ant.jar(我在网上找的jar包:ant-1.6.5.jar)

2、代码实现

import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;  /**
     * 导出附件
     * 
     * @return
     * @throws Exception
     */
    public String downloads() throws Exception {
        String idStr = getRequest().getParameter("id");
        if (StringUtils.isNotBlank(idStr)) {
            BadmarketApply apply = badmarketApplyDao.findByKey(Integer.parseInt(idStr));
       //存储所有文件的url
            List<String> fileList = new ArrayList<String>();
            FileManage fm1 = apply.getFm1();
            if (fm1 != null && fm1.getId() != null && fm1.getId() > 0) {
                fileList.add(fm1.getUrl());
            }
            FileManage fm2 = apply.getFm2();
            if (fm2 != null && fm2.getId() != null && fm2.getId() > 0) {
                fileList.add(fm2.getUrl());
            }
            FileManage fm3 = apply.getFm3();
            if (fm3 != null && fm3.getId() != null && fm3.getId() > 0) {
                fileList.add(fm3.getUrl());
            }
            FileManage fm4 = apply.getFm4();
            if (fm4 != null && fm4.getId() != null && fm4.getId() > 0) {
                fileList.add(fm4.getUrl());
            }
            FileManage fm5 = apply.getFm5();
            if (fm5 != null && fm5.getId() != null && fm5.getId() > 0) {
                fileList.add(fm5.getUrl());
            }
            FileManage fm6 = apply.getFm6();
            if (fm6 != null && fm6.getId() != null && fm6.getId() > 0) {
                fileList.add(fm6.getUrl());
            }
            FileManage fm7 = apply.getFm7();
            if (fm7 != null && fm7.getId() != null && fm7.getId() > 0) {
                fileList.add(fm7.getUrl());
            }
            FileManage fm8 = apply.getFm8();
            if (fm8 != null && fm8.getId() != null && fm8.getId() > 0) {
                fileList.add(fm8.getUrl());
            }
            FileManage fm9 = apply.getFm9();
            if (fm9 != null && fm9.getId() != null && fm9.getId() > 0) {
                fileList.add(fm9.getUrl());
            }
            FileManage fm10 = apply.getFm10();
            if (fm10 != null && fm10.getId() != null && fm10.getId() > 0) {
                fileList.add(fm10.getUrl());
            }
            FileManage fm11 = apply.getFm11();
            if (fm11 != null && fm11.getId() != null && fm11.getId() > 0) {
                fileList.add(fm11.getUrl());
            }
            FileManage fm12 = apply.getFm12();
            if (fm12 != null && fm12.getId() != null && fm12.getId() > 0) {
                fileList.add(fm12.getUrl());
            }
            FileManage fm13 = apply.getFm13();
            if (fm13 != null && fm13.getId() != null && fm13.getId() > 0) {
                fileList.add(fm13.getUrl());
            }
            FileManage fm14 = apply.getFm14();
            if (fm14 != null && fm14.getId() != null && fm14.getId() > 0) {
                fileList.add(fm14.getUrl());
            }
            FileManage fm15 = apply.getFm15();
            if (fm15 != null && fm15.getId() != null && fm15.getId() > 0) {
                fileList.add(fm15.getUrl());
            }
            //压缩包的名称
            String name = apply.getNo()+"-所有附件.zip";
            String zipFileName = new String(name.getBytes("gbk"), "iso8859-1"); // 此处可解决中文乱码问题
            String path = this.getServerUpload();
            getResponse().setContentType("application/x-msdownload"); // 通知客户文件的MIME类型:
            getResponse().setHeader("Content-disposition", "attachment;filename=" + zipFileName);

            ZipOutputStream zos = new ZipOutputStream(getResponse().getOutputStream());
       //所有文件写入压缩文件
            for (String filePath : fileList) {
                File file = new File(path + File.separator + filePath);
                doZip(file, zos);
            }
            zos.close();
        }
        return null;
    }

    /**
     *  处理批量下载时文件压缩问题
     * @param file
     * @param zos
     * @throws IOException
     */
    private void doZip(File file, ZipOutputStream zos) throws IOException {
        if (file.exists()) {
            if (file.isFile()) {
                // 如果是文件,写入到 zip 流中
                ZipEntry zet = new ZipEntry(file.getName());
                zos.putNextEntry(zet);
                FileInputStream fis = new FileInputStream(file);
                byte[] buffer = new byte[1024];
                int r = 0;
                while ((r = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, r);
                }
                zos.setEncoding("gbk"); // 这个地方很重要
                zos.flush();
                fis.close();
            }
        }
    }

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0