diff --git a/mes/hd/nladmin-system/pom.xml b/mes/hd/nladmin-system/pom.xml index fb8fdf0b..ffa3807e 100644 --- a/mes/hd/nladmin-system/pom.xml +++ b/mes/hd/nladmin-system/pom.xml @@ -26,6 +26,13 @@ + + + + net.coobird + thumbnailator + 0.4.8 + e-iceblue diff --git a/mes/hd/nladmin-system/src/main/java/org/nl/wms/common/util/ImageCompress.java b/mes/hd/nladmin-system/src/main/java/org/nl/wms/common/util/ImageCompress.java new file mode 100644 index 00000000..a36afc7c --- /dev/null +++ b/mes/hd/nladmin-system/src/main/java/org/nl/wms/common/util/ImageCompress.java @@ -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); + }; +} diff --git a/mes/hd/nladmin-system/src/main/java/org/nl/wms/ql/rest/FactorywarrantymstController.java b/mes/hd/nladmin-system/src/main/java/org/nl/wms/ql/rest/FactorywarrantymstController.java index fe22bdc8..a05dedbb 100644 --- a/mes/hd/nladmin-system/src/main/java/org/nl/wms/ql/rest/FactorywarrantymstController.java +++ b/mes/hd/nladmin-system/src/main/java/org/nl/wms/ql/rest/FactorywarrantymstController.java @@ -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 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();