Java批量导出图片,并压缩成zip

@Override
    public void exportQrCode(YardPcPageParam param, HttpServletResponse response) {
        
        QueryWrapper<YardEntity> queryWrapper = this.toPcListQuery(param);
        List<YardEntity> yardEntityList = this.list(queryWrapper);
        if (yardEntityList.size() == 0) {
            throw new BizException("暂无数据!");
        }
        List<YardSimpleVo> yardSimpleVos = yardEntityList.stream().map(e -> {
            YardSimpleVo vo = new YardSimpleVo();
            vo.setName(e.getGroupId() + "大组" + e.getHouseNumber());
            vo.setUrl(StringUtils.isEmpty(e.getQrCode()) ? "" : OssUtils.getUrlByOss(e.getQrCode()));
            return vo;
        }).collect(Collectors.toList());

        String zipFileName = "yardsQrCode.zip";
        //响应头的设置
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;filename=" + zipFileName);

        ZipOutputStream zipOutputStream = null;
        try {
            zipOutputStream = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
            zipOutputStream.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataOutputStream os = null;
        for (YardSimpleVo yardSimpleVo : yardSimpleVos) {
            if (StringUtils.isEmpty(yardSimpleVo.getUrl())) {
                continue;
            }
            String fileName = yardSimpleVo.getName() + ".jpg";
            File file = new File("");
            try {
                URL url = new URL(yardSimpleVo.getUrl());
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5000);
                InputStream is = conn.getInputStream();
                file = FileUtils.inputStreamToFile(is, fileName);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (file.exists()) {
                try {
                    assert zipOutputStream != null;
                    zipOutputStream.putNextEntry(new ZipEntry(fileName));
                    os = new DataOutputStream(zipOutputStream);
                    InputStream is = new FileInputStream(file);
                    byte[] b = new byte[100];
                    int length = 0;
                    while ((length = is.read(b)) != -1) {
                        os.write(b, 0, length);
                    }
                    is.close();
                    zipOutputStream.closeEntry();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            if (os != null) {
                os.flush();
                os.close();
            }
            if (zipOutputStream != null) {
                zipOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

需求:批量下载图片,并打包成zip导出

FileUtils:

import java.io.*;

public class FileUtils {
    /**
     * InputStream 转file
     */
    public static File inputStreamToFile(InputStream ins, String name) throws Exception {
        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
        if (file.exists()) {
            return file;
        }
        OutputStream os = new FileOutputStream(file);
        int bytesRead;
        int len = 8192;
        byte[] buffer = new byte[len];
        while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
        return file;
    }
}

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值