add:质保书上传图片添加压缩服务

This commit is contained in:
zhangzhiqiang
2022-11-18 17:08:52 +08:00
parent d4c949cdeb
commit 020850da61
3 changed files with 65 additions and 0 deletions

View File

@@ -26,6 +26,13 @@
</repositories>
<dependencies>
<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
<!-- Excel打印-->
<dependency>
<groupId>e-iceblue</groupId>

View File

@@ -0,0 +1,54 @@
package org.nl.wms.common.util;
import net.coobird.thumbnailator.Thumbnails;
import org.nl.exception.BadRequestException;
import org.nl.utils.FileUtil;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.math.BigDecimal;
/*
* @author ZZQ
* @Date 2022/11/18 4:17 下午
*/
public class ImageCompress {
public static Long imgCompress(File file,Long desFileSize,Double accuracy,int threshold){
if (threshold>5){
return file.length();
}
long length = file.length();
//小于目标压缩文件大小的2倍就不压缩了
if (length>desFileSize*1024*2){
try {
BufferedImage bim = ImageIO.read(file);
int imgWidth = bim.getWidth();
int imgHeight = bim.getHeight();
int desWidth = new BigDecimal(imgWidth).multiply(
new BigDecimal(accuracy)).intValue();
int desHeight = new BigDecimal(imgHeight).multiply(
new BigDecimal(accuracy)).intValue();
Thumbnails.of(file.getPath()).size(desWidth, desHeight).outputQuality(accuracy).toFile(file.getPath());
//如果不满足要求,递归直至满足要求
imgCompress(file, desFileSize, accuracy,threshold);
}catch (Exception ex){
ex.printStackTrace();
}
}
return file.length();
};
public static Long imgCompress(String path,Long desFileSize,Double accuracy){
File file;
try {
file = new File(path);
}catch (Exception ex){
throw new BadRequestException("文件不存在");
}
int i = 0;
//小于目标压缩文件大小的2倍就不压缩了
return imgCompress(file, desFileSize==null?100L:desFileSize, accuracy==null?0.8:accuracy,i);
};
}

View File

@@ -15,6 +15,7 @@ import org.nl.exception.BadRequestException;
import org.nl.service.LocalStorageService;
import org.nl.utils.FileUtil;
import org.nl.utils.SecurityUtils;
import org.nl.wms.common.util.ImageCompress;
import org.nl.wms.ql.Enum.QlBillStatusEnum;
import org.nl.wms.ql.service.FactorywarrantymstService;
import org.nl.wql.core.bean.WQLObject;
@@ -126,6 +127,9 @@ public class FactorywarrantymstController {
@ApiOperation("质保书上传")
public ResponseEntity<Object> upload(@RequestParam MultipartFile file, @PathVariable String inspection_id) {
LocalStorage localStorage = this.localStorageService.create((String) null, file);
Long compressSize = ImageCompress.imgCompress(localStorage.getPath(), 100L, 0.8);
localStorage.setSize(FileUtil.getSize(compressSize));
this.localStorageService.update(localStorage);
JSONObject mstObj = WQLObject.getWQLObject("ql_test_factorywarrantymst").query("inspection_id = '" + inspection_id + "'").uniqueResult(0);
if (ObjectUtil.isEmpty(mstObj)) throw new BadRequestException("未找到相关质保书!");
Long currentUserId = SecurityUtils.getCurrentUserId();