Java生成下载zip文件

标签:

本文出自jvm123.com-java技术分享站:http://jvm123.com/2020/05/java-sheng-cheng.html

Java生成下载zip文件,如下:

ZipOutputStream zipOs = new ZipOutputStream(response.getOutputStream());
downloadZip(zipOs, xmlName);
// 缺少这句,导出的文件少了100b
zipOs.close();
    // 生成zip文件
    protected void downloadZip(ZipOutputStream zipOut, String filename) {
        try {
            FileInputStream fis = new FileInputStream(PATH + filename);
            zipOut.putNextEntry(new ZipEntry(filename));
            IOUtils.copy(fis, zipOut);
            zipOut.closeEntry();
            //关闭输入流
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

发表评论