diff --git a/epmet-module/epmet-oss/epmet-oss-client/pom.xml b/epmet-module/epmet-oss/epmet-oss-client/pom.xml index 7e307f1777..3b07913b6e 100644 --- a/epmet-module/epmet-oss/epmet-oss-client/pom.xml +++ b/epmet-module/epmet-oss/epmet-oss-client/pom.xml @@ -33,6 +33,11 @@ commons-fileupload 1.3.3 + + net.coobird + thumbnailator + 0.4.8 + diff --git a/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/controller/OssController.java b/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/controller/OssController.java index 1b79042fbc..b265308eaf 100644 --- a/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/controller/OssController.java +++ b/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/controller/OssController.java @@ -148,6 +148,19 @@ public class OssController { return ossService.uploadImg(file, null); } + /** + * 压缩上传 + * + * @param file + * @return com.epmet.commons.tools.utils.Result + * @author zhy + * @date 2022/5/24 13:32 + */ + @PostMapping("compressuploadimg") + public Result compressUploadImg(@RequestParam("file") MultipartFile file) { + return ossService.compressUploadImg(file, null); + } + @PostMapping("uploadwximg") public Result uploadWxImg(@RequestPart("media") MultipartFile media) { return ossService.uploadImg(media, null); diff --git a/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/service/OssService.java b/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/service/OssService.java index ae9625ee66..d9e210c006 100644 --- a/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/service/OssService.java +++ b/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/service/OssService.java @@ -31,6 +31,8 @@ public interface OssService extends BaseService { Result uploadImg(MultipartFile file, String privacy); + Result compressUploadImg(MultipartFile file, String privacy); + Result extUpload(MultipartFile file, String fileName, String privacy); Result uploadVariedFile(MultipartFile file); diff --git a/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/service/impl/OssServiceImpl.java b/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/service/impl/OssServiceImpl.java index 6fab1be511..1e0c8800e6 100644 --- a/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/service/impl/OssServiceImpl.java +++ b/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/service/impl/OssServiceImpl.java @@ -29,6 +29,7 @@ import com.epmet.entity.OssEntity; import com.epmet.exception.ModuleErrorCode; import com.epmet.service.OssService; import lombok.extern.slf4j.Slf4j; +import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -37,8 +38,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.math.BigDecimal; import java.util.Map; @Slf4j @@ -94,6 +100,37 @@ public class OssServiceImpl extends BaseServiceImpl implement return new Result().ok(dto); } + @Override + public Result compressUploadImg(MultipartFile file, String privacy) { + if (file.isEmpty()) { + return new Result().error(ModuleErrorCode.UPLOAD_FILE_EMPTY); + } + //上传文件 + String extension = FilenameUtils.getExtension(file.getOriginalFilename()); + String url = null; + String ossDomain = null; + try { + byte[] fileBytes = compressPicCycle(file.getBytes(),200L,0.7); + AbstractCloudStorageService storageService = OssFactory.build(); + url = storageService.uploadSuffix(fileBytes, extension, privacy); + ossDomain = storageService.getOssDomain(privacy); + } catch (IOException e) { + logger.error("图片上传异常", e); + throw new RenException("图片上传异常"); + + } + //保存文件信息 + OssEntity ossEntity = new OssEntity(); + ossEntity.setUrl(url); + + baseDao.insert(ossEntity); + //文件信息 + UploadImgResultDTO dto = new UploadImgResultDTO(); + dto.setUrl(url); + dto.setDomain(ossDomain); + return new Result().ok(dto); + } + @Override public Result extUpload(MultipartFile file, String fileName, String privacy) { try { @@ -262,5 +299,33 @@ public class OssServiceImpl extends BaseServiceImpl implement return ossDomain.concat(File.separator).concat(ossPrefix).concat(File.separator).concat(filePath); } + /** + * + * @param bytes 原图片字节数组 + * @param desFileSize 指定图片大小,单位 kb + * @param accuracy 精度,递归压缩的比率,建议小于0.9 + * @return + */ + private byte[] compressPicCycle(byte[] bytes, long desFileSize, double accuracy) throws IOException{ + // 获取目标图片 + long fileSize = bytes.length; + System.out.println("=====fileSize======== "+fileSize); + // 判断图片大小是否小于指定图片大小 + if(fileSize <= desFileSize * 1024){ + return bytes; + } + //计算宽高 + BufferedImage bim = ImageIO.read(new ByteArrayInputStream(bytes)); + int imgWidth = bim.getWidth(); + System.out.println(imgWidth+"====imgWidth====="); + int imgHeight = bim.getHeight(); + int desWidth = new BigDecimal(imgWidth).multiply( new BigDecimal(accuracy)).intValue(); + System.out.println(desWidth+"====desWidth====="); + int desHeight = new BigDecimal(imgHeight).multiply( new BigDecimal(accuracy)).intValue(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字节输出流(写入到内存) + Thumbnails.of(new ByteArrayInputStream(bytes)).size(desWidth, desHeight).outputQuality(accuracy).toOutputStream(baos); + //如果不满足要求,递归直至满足要求 + return compressPicCycle(baos.toByteArray(), desFileSize, accuracy); + } }