技师上传头像压缩

This commit is contained in:
曹磊 2025-02-11 14:42:51 +08:00
parent 6dd59f27cd
commit 83f47aa6f8
1 changed files with 8 additions and 1 deletions

View File

@ -175,6 +175,7 @@ public class FileUploadUtils
//如果是图片则压缩 //如果是图片则压缩
if(isImg){ if(isImg){
try { try {
Date date = new Date(); Date date = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
Random random = new Random(); Random random = new Random();
@ -182,7 +183,7 @@ public class FileUploadUtils
String outfilename = fileName.substring(0,fileName.lastIndexOf("/"))+"/"+sdf.format(date)+randomNumber+fileName.substring(fileName.lastIndexOf("."),fileName.length()); String outfilename = fileName.substring(0,fileName.lastIndexOf("/"))+"/"+sdf.format(date)+randomNumber+fileName.substring(fileName.lastIndexOf("."),fileName.length());
File inputFile = new File(baseDir + File.separator + fileName); File inputFile = new File(baseDir + File.separator + fileName);
File outputFile = new File(baseDir + File.separator + outfilename); // 输出图片文件 File outputFile = new File(baseDir + File.separator + outfilename); // 输出图片文件
scaledImage(inputFile, outputFile,300 ,500); scaledImage(inputFile, outputFile,0 ,0);
pathFileName = "/file/uploadPath/"+outfilename; pathFileName = "/file/uploadPath/"+outfilename;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -193,6 +194,12 @@ public class FileUploadUtils
public static void scaledImage(File inputFile, File outputFile, int targetWidth, int targetHeight) throws IOException { public static void scaledImage(File inputFile, File outputFile, int targetWidth, int targetHeight) throws IOException {
BufferedImage bufferedImage = ImageIO.read(inputFile); BufferedImage bufferedImage = ImageIO.read(inputFile);
if(targetWidth==0){
targetWidth = bufferedImage.getWidth();
}
if(targetHeight==0){
targetHeight = bufferedImage.getHeight();
}
Image compressedImage = bufferedImage.getScaledInstance(targetWidth,targetHeight,Image.SCALE_SMOOTH); Image compressedImage = bufferedImage.getScaledInstance(targetWidth,targetHeight,Image.SCALE_SMOOTH);
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
// 绘制缩放后的图片 // 绘制缩放后的图片