栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA基础必备功能之导出ZIP文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA基础必备功能之导出ZIP文件

导出ZIP文件

比较常用的两种:
导出图片压缩文件,导出excel压缩文件;

导出思路:需要导出的文件转存为byte数组保存到Map,然后遍历压缩成zip

需要引入jar:



	org.apache.commons
	commons-compress
	1.12

一、导出图片压缩文件
    
    public static byte[] imgageToByte(String imageUrl, String imageName) throws IOException {
        File imageFile = new File(imageUrl + File.separator + imageName);
        if(!imageFile.exists()){
            log.error("---imgageToByte--imageUrl([{}])--is--null", imageUrl);
            return null;
        }
//        return Files.readAllBytes(imageFile.toPath()); // 不推荐
        FileInputStream fis = new FileInputStream(imageFile);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        byte[] imagetByte = bos.toByteArray();
        fis.close();
        bos.close();
        return imagetByte;
    }

    
    public static void fileTozip(HttpServletResponse response, String zipName, Map zipMap) {
        try{
            response.setHeader("Content-Disposition", "attachment; filename=" + zipName);
            ServletOutputStream outputStream = response.getOutputStream();
            ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(outputStream);
            zipOutputStream.setUseZip64(Zip64Mode.AsNeeded);
            for(String fileName: zipMap.keySet()){
                ArchiveEntry archiveEntry = new ZipArchiveEntry(fileName);
                zipOutputStream.putArchiveEntry(archiveEntry);
                byte[] fileByte = zipMap.get(fileName);
                zipOutputStream.write(fileByte);
                zipOutputStream.closeArchiveEntry();
            }
            zipOutputStream.close();
            outputStream.close();
        }catch (Exception e){
            log.error("fileTozip error:[{}]", ExceptionUtils.getStackTrace(e));
        }
    }
二、导出excel压缩文件

把csv文件转为byte数组,然后再通过fileTozip方法输出zip文件即可

public static byte[] writeCsvAfterToBytes(String[] headers,List cellList) {
    byte[] bytes = new byte[0];
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, Charset.forName("UTF-8"));
    BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
    CSVPrinter csvPrinter = null;
    try {
        //创建csvPrinter并设置表格头
        csvPrinter = new CSVPrinter(bufferedWriter, CSVFormat.DEFAULT.withHeader(headers));
        //写数据
        csvPrinter.printRecords(cellList);
        csvPrinter.flush();
        bytes = byteArrayOutputStream.toString("UTF-8").getBytes();
    } catch (IOException e) {
        log.error("writeCsv IOException:{}", e.getMessage(), e);
    } finally {
        try {
            if (csvPrinter != null) {
                csvPrinter.close();
            }
            if (bufferedWriter != null) {
                bufferedWriter.close();
            }
            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }
            if (byteArrayOutputStream != null) {
                byteArrayOutputStream.close();
            }
        } catch (IOException e) {
            log.error("iostream close IOException:{}", e.getMessage(), e);
        }
    }
    return bytes;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1031837.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号