根据图片url批量下载图片(以zip格式存储)

controll层

 /**
         * 批量下载照片
         */
        @RequestMapping("/downloadJSImg/{ids}")
        public void downJSImg(@PathVariable String[] ids){
            koiCarpInfoService.downloadJSImg(ids);
        }

service层 

/**
     * 标准照下载
     * @author csz
     * @param 选中的数据 标准照下载
     */
    @Override
    public void downloadJSImg(String[] ids) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());

        //根据ids先查询符合条件的数据
        List<KoiCarpInfo> list = koiCarpInfoMapper.selectDownloadImgs(ids);
        //声明一个存储路径的List
        List<Map<String, String>> paths = new ArrayList<Map<String, String>>();
        list.forEach(item -> {
            String imagePrefix = item.getCompanyname() + "-" + item.getName();
            paths.add(GenerateImageUtil.GenerateImage(GenerateImageUtil.ByteStreamToBase64(ImageToBytes(item.getImgUrl())),imagePrefix));
        });
        try {
            DownloadZipUtil.download(paths,format+"锦鲤标准照",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     *  通过图片路径将图片文件转化为字符数组
     * @author csz
     * @param url 图片路径
     * @return byte[]
     */
    public static byte[] ImageToBytes(String url){
        try {
            URL urlConet = new URL(url);
            HttpURLConnection con = (HttpURLConnection)urlConet.openConnection();
            con.setRequestMethod("GET");
            con.setConnectTimeout(4 * 1000);
            InputStream inStream = con .getInputStream();    //通过输入流获取图片数据
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[2048];
            int len = 0;
            while( (len=inStream.read(buffer)) != -1 ){
                outStream.write(buffer, 0, len);
            }
            inStream.close();
            byte[] data =  outStream.toByteArray();
            return data;
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

生成图片工具类

package com.datacreateai.koi.util;

import com.datacreateai.common.utils.StringUtils;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
/**
 * 生成图片工具类
 * Created by gs01 on 2022-09-07.
 */
public class GenerateImageUtil {

    /**
     * Base64字符串进行解码并生成图片
     *
     */
    public static HashMap<String, String> GenerateImage(String imgStr, String namingFormat) {
        if (imgStr == null) // 图像数据为空
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            //生成图片名称
            String imgFilePath;
            if (StringUtils.isNotBlank(namingFormat)){
                imgFilePath = "d://"+namingFormat+".jpg";
            }else{
                imgFilePath = "d://"+System.currentTimeMillis()+".jpg";
            }
            //将生成的图片的路径存到Map集合
            HashMap<String, String> map = new HashMap<>();
            map.put("filePath",imgFilePath);
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
//            return true;
            return map;
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 字节流转Base64
     *
     */
    public static String ByteStreamToBase64(byte[] array){
        String imgBase64 = "";
        byte[] zpBlob = array;
        if (zpBlob != null) {
            byte[] encodeBase64 = Base64.encodeBase64(zpBlob);
            //Base64图片
            imgBase64 = new String((encodeBase64));
        }
        return imgBase64;
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值