Commit 6b477fb4 authored by yaobaizheng's avatar yaobaizheng

图片过大进行二次压缩

parent b1329054
package com.lyy.admin.common.utils.hwobs;
import cn.hutool.core.img.Img;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
//import com.xiruo.medbid.util.OssFileUtils;
//import net.coobird.thumbnailator.Thumbnails;
//import org.apache.commons.io.FileUtils;
//import org.apache.http.client.utils.URIBuilder;
import java.io.*;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
public class PicUtils {
public static void main(String[] args) throws Exception {
// String url = "https://mbnprodfile.oss-cn-shenzhen.aliyuncs.com/xxxxx/5868500806729729.jpg";
// System.err.println(zipPic(url, 180));
// String pc_url = "C:\\Users\\26996\\Pictures\\Screenshots\\1704418796(1) - 副本.jpg";
// System.err.println(zipPicPC(pc_url, 400));
String pc_url = "C:\\Users\\26996\\Pictures\\Screenshots\\微信图片_20240105112648 - 副本.jpg";
String pc_url1 = "C:\\Users\\26996\\Pictures\\Screenshots\\1704418796(1) - 副本1.jpg";
File file = new File(pc_url);
File file1 = new File(pc_url1);
compressPicForScale1(file,file1,800);
}
public static void disposeFile(MultipartFile files, String localDir, String localDir1 ) throws IOException {
//将MultipartFile转为File,存储为本地临时文件
File file = new File(localDir);
InputStream inputStream = files.getInputStream();
FileUtils.copyInputStreamToFile(inputStream, file);
//目标文件
File file1 = FileUtil.file(localDir1);
compressPicForScale1(file, file1, 800);
}
/**
* 根据指定大小压缩图片
*
* @param file 源图片字节数组
* @param newFile 源图片字节数组
* @param desFileSize 指定图片大小,单位kb
* @return 压缩质量后的图片字节数组
*/
public static void compressPicForScale1(File file, File newFile, long desFileSize) throws IOException {
long fileLength = file.length();
if ( fileLength <= 0 || fileLength < desFileSize * 1024) {
return ;
}
Img.from(file).setQuality(1).write(newFile);
if(newFile.length() > desFileSize * 1024){
double accuracy = getAccuracy(newFile.length() / 1024);
Img.from(newFile).setQuality(accuracy).write(newFile);
}
}
/**
* 自动调节精度(经验数值)
*
* @param size 源图片大小
* @return 图片压缩质量比
*/
private static double getAccuracy(long size) {
double accuracy = 1 ;
if (size >= 800 && size < 1024) {
accuracy = 0.6;
}
if (size >= 1024 && size < 2048) {
accuracy = 0.3;
}
if (size >= 2048) {
accuracy = 0.1;
}
return accuracy;
}
public static byte[] readInputStream(InputStream inStream) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用一个输入流从buffer里把数据读取出来
while ((len = inStream.read(buffer)) != -1) {
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
//把outStream里的数据写入内存
return outStream.toByteArray();
}
}
......@@ -7,6 +7,7 @@ import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.lyy.admin.common.utils.hwobs.ObsUtils;
import com.lyy.admin.common.utils.hwobs.PicUtils;
import com.lyy.admin.moudle.system.service.UploadService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
......@@ -62,37 +63,28 @@ public class UploadServiceImpl implements UploadService {
}else{
String newFile = IdUtil.simpleUUID();
String newFilename = newFile + fileLastType;
TimeInterval timer = DateUtil.timer();
//将MultipartFile转为File,存储为本地临时文件
//源文件路径
String localDir = tempFilePath + newFilename;
File file = new File(localDir);
InputStream inputStream = files.getInputStream();
FileUtils.copyInputStreamToFile(inputStream, file);
//压缩之后的文件路径
String newFilename1 = IdUtil.simpleUUID() + ".jpg";
String localDir1 = tempFilePath + newFilename1;
File file1 = FileUtil.file(localDir1);
System.out.println("图片存储与新建"+timer.interval());
TimeInterval timer1 = DateUtil.timer();
Img.from(file).setQuality(1).write(file1);
System.out.println("图片压缩"+timer1.interval());
//压缩文件处理
TimeInterval timer1 = DateUtil.timer();
PicUtils.disposeFile(files, localDir, localDir1);
System.out.println("图片压缩处理:"+timer1.interval());
//上传文件
TimeInterval timer2 = DateUtil.timer();
InputStream inputStream1 = new FileInputStream(localDir1);
filePath = "/" + date + "/" + newFile + ".jpg" ;
obsUtils.ObsUpload(bucketName, rootPath + filePath, inputStream1);
System.out.println("图片上传"+timer2.interval());
System.out.println("图片上传"+timer2.interval());
//删除临时文件
CompletableFuture.runAsync(()-> {
try {
FileUtils.delete(file);
FileUtils.delete(file1);
} catch (IOException e) {
throw new RuntimeException(e);
}
FileUtil.del(localDir);
FileUtil.del(localDir1);
});
inputStream.close();
inputStream1.close();
}
return rootPath + filePath;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment