257 lines
11 KiB
Java
257 lines
11 KiB
Java
package gb;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import io.minio.MinioClient;
|
||
import io.minio.PutObjectOptions;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import net.coobird.thumbnailator.Thumbnails;
|
||
import net.shapelight.AdminApplication;
|
||
import net.shapelight.common.config.MinioConfig;
|
||
import net.shapelight.common.utils.*;
|
||
import net.shapelight.modules.vo.CarImageCache;
|
||
import org.junit.Test;
|
||
import org.junit.runner.RunWith;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.boot.test.context.SpringBootTest;
|
||
import org.springframework.test.context.junit4.SpringRunner;
|
||
|
||
import javax.imageio.ImageIO;
|
||
import java.awt.image.BufferedImage;
|
||
import java.io.*;
|
||
import java.net.HttpURLConnection;
|
||
import java.net.URL;
|
||
import java.util.Date;
|
||
import java.util.Map;
|
||
import java.util.Random;
|
||
|
||
|
||
@RunWith(SpringRunner.class)
|
||
@SpringBootTest(classes = AdminApplication.class)
|
||
@Slf4j
|
||
public class CarImageTest {
|
||
@Autowired
|
||
private MinioClient minioClient;
|
||
@Autowired
|
||
private MinioConfig minioConfig;
|
||
@Autowired
|
||
private RedisUtils redisUtils;
|
||
|
||
@Test
|
||
public void ossTest() {
|
||
|
||
// String imgUrl = "http://icecloud-prod.oss-cn-shenzhen.aliyuncs.com/P1613901148/image/202106/17/%E9%99%95A3U32K_out_2_4472.jpg?Expires=1623895438&OSSAccessKeyId=LTAI4Furp7fmTGDMRhJ95jaX&Signature=iJ7H8%2FmOQz2Zs81APGk2SMrREGQ%3D";
|
||
// imgUrl = imgUrl.replace("amp;", "");
|
||
// String fileName = "car/" + 222 + "/" + UUIDUtil.uuid() + ".jpg";
|
||
// //缓存
|
||
// JSONObject cache = new JSONObject();
|
||
// cache.put("fileName",fileName);
|
||
// cache.put("imgUrl",imgUrl);
|
||
// redisUtils.set("CarImage:"+fileName,cache,1500l);//保存25分钟60*25
|
||
|
||
|
||
|
||
//缓存
|
||
CarImageCache carImageCache = new CarImageCache();
|
||
carImageCache.setFileName("car/P2/202107/22222.jpg");
|
||
carImageCache.setImgUrl("http://icecloud-prod.oss-cn-shenzhen.aliyuncs.com/P1619325610/image/202107/12/172151_in_1_%E9%99%95A063Y8_38.jpg?Expires=1626083513&OSSAccessKeyId=LTAI4Furp7fmTGDMRhJ95jaX&Signature=0V5rvWbjp%2BEIAHuBegDrRubhGJo%3D");
|
||
redisUtils.set("CarImage:"+"car/P2/202107/"+ UUIDUtil.uuid()+".jpg",carImageCache,1500l);//保存25分钟60*25
|
||
|
||
// JSONObject cacheObject = redisUtils.getPattern("CarImage");
|
||
// CarImageCache cacheObject = (CarImageCache)redisUtils.get("CarImage:car/P2/202107/100001.jpg");
|
||
CarImageCache cacheObject = (CarImageCache)redisUtils.getPattern("CarImage*");
|
||
String imgUrl = cacheObject.getImgUrl();
|
||
String fileName = cacheObject.getFileName();
|
||
if (!StringUtils.isEmpty(imgUrl)) {
|
||
URL url = null;
|
||
InputStream is = null;
|
||
HttpURLConnection httpUrl = null;
|
||
try {
|
||
url = new URL(imgUrl);
|
||
|
||
BufferedImage image = ImageIO.read(url);
|
||
//获取图片的宽、高
|
||
System.out.println("Width: " + image.getWidth());
|
||
System.out.println("Height: " + image.getHeight());
|
||
int w = image.getWidth();
|
||
int h = image.getHeight();
|
||
//调整尺寸不能大于1024
|
||
if(w>1024 || h>1024){
|
||
image = ThumbnailsUtils.resizeImageOne(image,1024,1024);
|
||
}
|
||
byte[] data = ImageUtils.imageToBytes(image);
|
||
is = new ByteArrayInputStream(data);
|
||
int rl = is.available();
|
||
PutObjectOptions putObjectOptions = new PutObjectOptions(rl, -1L);
|
||
putObjectOptions.setContentType("image/jpeg");
|
||
this.minioClient.putObject(this.minioConfig
|
||
.getBucketName(), fileName, is, putObjectOptions);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
// log.error("保存图片失败:"+imgUrl+":"+e.getMessage());
|
||
fileName = "";
|
||
} finally {
|
||
if (is != null)
|
||
try {
|
||
is.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
if (httpUrl != null)
|
||
httpUrl.disconnect();
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
// String imgUrl = "http://icecloud-prod.oss-cn-shenzhen.aliyuncs.com/P1610092525/image/202104/25/%E9%99%95A207AY_out_2_5802.jpg?Expires=1619330640&OSSAccessKeyId=LTAI4Furp7fmTGDMRhJ95jaX&Signature=2xS3T%2FlK9LMe%2FjAOotCEvIlCOmU%3D";
|
||
// imgUrl = imgUrl.replace("amp;", "");
|
||
// String fileName = "car/" + 222 + "/" + UUIDUtil.uuid() + ".jpg";
|
||
// if (!StringUtils.isEmpty(imgUrl)) {
|
||
// URL url = null;
|
||
// InputStream is = null;
|
||
// HttpURLConnection httpUrl = null;
|
||
// try {
|
||
// url = new URL(imgUrl);
|
||
//
|
||
//
|
||
// BufferedImage image = ImageIO.read(url);
|
||
// //获取图片的宽、高
|
||
// System.out.println("Width: " + image.getWidth());
|
||
// System.out.println("Height: " + image.getHeight());
|
||
// //调整图片大小为 400X400尺寸
|
||
//// BufferedImage newImage = ImageUtils.resizeImage(image,400,400);
|
||
// int w = image.getWidth();
|
||
// int h = image.getHeight();
|
||
// if(w>1024 || h>1024){
|
||
// image = ThumbnailsUtils.resizeImageOne(image,1024,1024);
|
||
// }
|
||
//
|
||
//
|
||
//// BufferedImage resizeBuff = ThumbnailsUtils.resizeImageOne(image,1024,1024);
|
||
//
|
||
//
|
||
//// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||
//// if (w > 1024 || h > 1024) {
|
||
//// Thumbnails.of(image)
|
||
//// .size(1024, 1024)
|
||
////// .outputFormat("JPEG")
|
||
////// .outputQuality(1)
|
||
//// .toOutputStream(outputStream);
|
||
//// }
|
||
//
|
||
// byte[] data = ImageUtils.imageToBytes(image);
|
||
//// ByteArrayInputStream
|
||
// is = new ByteArrayInputStream(data);
|
||
// int rl = is.available();
|
||
//
|
||
//
|
||
//// httpUrl = (HttpURLConnection) url.openConnection();
|
||
//// httpUrl.connect();
|
||
////// httpUrl.getInputStream();
|
||
//// is = httpUrl.getInputStream();
|
||
//// int rl = httpUrl.getContentLength();
|
||
//// int l = is.available();
|
||
// PutObjectOptions putObjectOptions = new PutObjectOptions(rl, -1L);
|
||
// putObjectOptions.setContentType("image/jpeg");
|
||
// this.minioClient.putObject(this.minioConfig
|
||
// .getBucketName(), fileName, is, putObjectOptions);
|
||
// } catch (Exception e) {
|
||
// e.printStackTrace();
|
||
// fileName = "";
|
||
// } finally {
|
||
// if (is != null)
|
||
// try {
|
||
// is.close();
|
||
// } catch (IOException e) {
|
||
// e.printStackTrace();
|
||
// }
|
||
// if (httpUrl != null)
|
||
// httpUrl.disconnect();
|
||
// }
|
||
// }
|
||
}
|
||
|
||
|
||
@Test
|
||
public void imageUrlTest() {
|
||
String imgUrl = "http://icecloud-prod.oss-cn-shenzhen.aliyuncs.com/P1613901148/image/202106/17/%E9%99%95AH345%E5%AD%A6_in_2_9660.jpg?Expires=1623901253&OSSAccessKeyId=LTAI4Furp7fmTGDMRhJ95jaX&Signature=ASDVTMGzPt8INggoslRbYBgyenc%3D";
|
||
imgUrl = imgUrl.replace("amp;", "");
|
||
String fileName = "car/" + 222 + "/" + UUIDUtil.uuid() + ".jpg";
|
||
|
||
log.error("开始执行");
|
||
|
||
if (!StringUtils.isEmpty(imgUrl)) {
|
||
URL url = null;
|
||
InputStream is = null;
|
||
// HttpURLConnection httpUrl = null;
|
||
try {
|
||
|
||
//new一个URL对象
|
||
url = new URL(imgUrl);
|
||
//打开链接
|
||
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||
//设置请求方式为"GET"
|
||
conn.setRequestMethod("GET");
|
||
//超时响应时间为5秒
|
||
conn.setConnectTimeout(5 * 1000);
|
||
//通过输入流获取图片数据
|
||
InputStream inStream = conn.getInputStream();
|
||
|
||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||
//创建一个Buffer字符串
|
||
byte[] buffer = new byte[1024000];
|
||
//每次读取的字符串长度,如果为-1,代表全部读取完毕
|
||
int len = 0;
|
||
//使用一个输入流从buffer里把数据读取出来
|
||
while( (len=inStream.read(buffer)) != -1 ){
|
||
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
|
||
outStream.write(buffer, 0, len);
|
||
}
|
||
|
||
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
|
||
byte[] dataImage = outStream.toByteArray();
|
||
|
||
//关闭输入流
|
||
inStream.close();
|
||
outStream.close();
|
||
|
||
ByteArrayInputStream bais = new ByteArrayInputStream(dataImage);
|
||
BufferedImage image = ImageIO.read(bais);
|
||
|
||
//获取图片的宽、高
|
||
// System.out.println("Width: " + image.getWidth());
|
||
// System.out.println("Height: " + image.getHeight());
|
||
int w = image.getWidth();
|
||
int h = image.getHeight();
|
||
//调整尺寸不能大于1024
|
||
if(w>1024 || h>1024){
|
||
image = ThumbnailsUtils.resizeImageOne(image,1024,1024);
|
||
}
|
||
byte[] data = ImageUtils.imageToBytes(image);
|
||
is = new ByteArrayInputStream(data);
|
||
int rl = is.available();
|
||
PutObjectOptions putObjectOptions = new PutObjectOptions(rl, -1L);
|
||
putObjectOptions.setContentType("image/jpeg");
|
||
this.minioClient.putObject(this.minioConfig
|
||
.getBucketName(), fileName, is, putObjectOptions);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
log.error("保存图片失败:"+imgUrl+"-----------------"+e.getMessage());
|
||
fileName = "";
|
||
} finally {
|
||
if (is != null)
|
||
try {
|
||
is.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
// if (httpUrl != null)
|
||
// httpUrl.disconnect();
|
||
}
|
||
}
|
||
|
||
log.error("结束执行");
|
||
}
|
||
}
|