图片打包下载工具类,java图片打包下载,图片压缩包下载

1.controller

  /**
     * 导出图片
     *
     * @param identifyInfoVo
     * @return
     */
    @AutoLog(value = "识别信息-导出图片")
    @ApiOperation(value = "识别信息-导出图片", notes = "识别信息-导出图片")
    @GetMapping(value = "/exportImg")
    public void exportImg(IdentifyInfoVo identifyInfoVo, HttpServletResponse response) throws IOException {
        identifyInfoService.exportImg(identifyInfoVo, response);
    }

2.service

    void exportImg(IdentifyInfoVo identifyInfoVo, HttpServletResponse response) throws IOException;

3.impl

 @Override
    public void exportImg(IdentifyInfoVo identifyInfoVo, HttpServletResponse response) throws IOException {
        List<String> imgUrls = new ArrayList<String>();
        List<IdentifyInfo> list = this.baseMapper.exportImgList(identifyInfoVo);
        //构建图片地址数组
        for (IdentifyInfo identifyInfo : list) {
            if (StringUtils.isNotEmpty(identifyInfo.getImg())) {
                imgUrls.add(upLoadPath + "/" + identifyInfo.getImg());
            }
        }
        ZipUtils.zipDownLoad(ImageExporter.exportImages(imgUrls), response);
    }

4.ImageExporter

package org.jeecg.common.util.shenxiu;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.poi.util.IOUtils;

import java.io.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ImageExporter
 * @Description: 图片打包导出
 * @Author: zhanghui
 * @Date: 2023-11-22
 * @Version: 1.0
 **/
public class ImageExporter {
    /**
     * 导出图片并打包为 zip 文件
     * @param imageFilePaths 包含图片文件路径的列表
     * @return 包含所有图片文件的 zip 文件的字节数组
     */
    public static byte[] exportImages(List<String> imageFilePaths) {
        // 将图片文件路径列表转换为 File 对象列表
        List<File> imageFiles = convertToFiles(imageFilePaths);

        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
             ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream)) {

            // 遍历每个图片文件,将其添加到 zip 文件中
            for (File imageFile : imageFiles) {
                addFileToZip(zipOutputStream, imageFile);
            }

            // 完成 zip 文件的创建
            zipOutputStream.finish();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将文件路径列表转换为 File 对象列表
     * @param filePaths 文件路径列表
     * @return 对应的 File 对象列表
     */
    private static List<File> convertToFiles(List<String> filePaths) {
        List<File> files = new ArrayList<>();
        for (String filePath : filePaths) {
            files.add(new File(filePath));
        }
        return files;
    }

    /**
     * 将单个文件添加到 zip 文件中
     * @param zipOutputStream ZipOutputStream 对象
     * @param file 要添加的文件
     * @throws IOException 发生 I/O 异常时抛出
     */
    private static void addFileToZip(ZipArchiveOutputStream zipOutputStream, File file) throws IOException {
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            // 创建 zip 文件中的条目,并设置条目名称
            ZipArchiveEntry zipEntry = new ZipArchiveEntry(file.getName());
            zipOutputStream.putArchiveEntry(zipEntry);

            // 将文件内容复制到 zip 文件中
            IOUtils.copy(fileInputStream, zipOutputStream);

            // 关闭条目
            zipOutputStream.closeArchiveEntry();
        }
    }

    /**
     * 将字节数组保存到本地文件
     * @param data 要保存的字节数组
     * @param filePath 保存的文件路径
     */
    public static void saveZipToLocal(byte[] data, String filePath) {
        try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
            fileOutputStream.write(data);
            System.out.println("Zip 文件已成功保存到:" + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        List<String> imageFiles = new ArrayList<>();
        imageFiles.add("/Users/engine/Desktop/1.png");
        imageFiles.add("/Users/engine/Desktop/2.png");
        imageFiles.add("/Users/engine/Desktop/3.png");
        byte[] zipBytes = exportImages(imageFiles);

        // 保存 zip 文件到本地
        saveZipToLocal(zipBytes, "/Users/engine/Desktop/exportImg.zip");
    }
}

5.ZipUtils

package org.jeecg.common.util.shenxiu;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

public class ZipUtils {


    public static void zipDownLoad(byte[] zipBytes, HttpServletResponse response) throws IOException {

        // 设置响应头
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"图片列表.zip\"");

        // 获取输出流
        try (OutputStream out = response.getOutputStream()) {
            // 写入字节数组到输出流
            out.write(zipBytes);
        }
    }
}

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值