java 上传图片压缩

 public static void uploadFile(MultipartFile multfile, String filePath) throws Exception {
File targetFile = new File(filePath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
//按比例压缩
Thumbnails.of(multfile.getInputStream()).scale(0.4f).outputQuality(0.25f).toFile(filePath);//filePath:上传图片的路径eg: xxx/xxx/xxx.jpg
//按尺寸压缩
//Thumbnails.of(multfile.getInputStream()).size(100,100).keepAspectRatio(false).toFile(filePath);
} catch (Exception e) {
log.info("压缩图片出现异常:", e);
}
}

<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
原文地址:https://www.cnblogs.com/austinspark-jessylu/p/9408479.html