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;
}
}