فهرست منبع

取消后端图片上传压缩

wanxl 1 سال پیش
والد
کامیت
e4081a1814
2فایلهای تغییر یافته به همراه56 افزوده شده و 63 حذف شده
  1. 5 5
      pom.xml
  2. 51 58
      src/main/java/com/sqx/modules/file/AliFileUploadController.java

+ 5 - 5
pom.xml

@@ -441,11 +441,11 @@
             <version>8.5.9</version>
         </dependency>
         <!--图片压缩webp-->
-        <dependency>
-            <groupId>org.sejda.imageio</groupId>
-            <artifactId>webp-imageio</artifactId>
-            <version>0.1.6</version>
-        </dependency>
+<!--        <dependency>-->
+<!--            <groupId>org.sejda.imageio</groupId>-->
+<!--            <artifactId>webp-imageio</artifactId>-->
+<!--            <version>0.1.6</version>-->
+<!--        </dependency>-->
     </dependencies>
 
     <dependencyManagement>

+ 51 - 58
src/main/java/com/sqx/modules/file/AliFileUploadController.java

@@ -3,7 +3,6 @@ package com.sqx.modules.file;
 import cn.hutool.core.util.StrUtil;
 import com.aliyun.oss.OSS;
 import com.aliyun.oss.OSSClientBuilder;
-import com.luciad.imageio.webp.WebPWriteParam;
 import com.sqx.common.exception.SqxException;
 import com.sqx.common.utils.Result;
 import com.sqx.config.MinioConfig;
@@ -33,13 +32,6 @@ import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
-import javax.imageio.IIOImage;
-import javax.imageio.ImageIO;
-import javax.imageio.ImageWriteParam;
-import javax.imageio.ImageWriter;
-import javax.imageio.stream.FileImageOutputStream;
-import javax.imageio.stream.ImageOutputStream;
-import java.awt.image.BufferedImage;
 import java.io.*;
 import java.net.URLDecoder;
 import java.nio.file.Files;
@@ -93,60 +85,61 @@ public class AliFileUploadController {
             // Minio上传
             String suffix = file.getOriginalFilename().substring(Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf("."));
             String completePath = getPath(suffix);
-            long fileSize =file.getSize();
+//            long fileSize =file.getSize();
             try {
-                InputStream inputStream=file.getInputStream();
-                if(this.isPicture(suffix)&&file.getSize()>1024*1024*2){
-                    completePath=getPath(".webp");
-                    String tmpPath =UUID.randomUUID().toString().replaceAll("-", "")+".webp";
-                    ApplicationHome applicationHome = new ApplicationHome(AliFileUploadController.class);
-                    String pathResouce = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath();
-                    String decode = URLDecoder.decode(pathResouce, "utf-8");
-                    File newFile = new File(decode + tmpPath);
-                    ImageOutputStream  ios=null;
-                    try {
-                        BufferedImage image = ImageIO.read(inputStream);
-                        ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();
-                        WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
-                        writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
-                        // 设置有损压缩
-                        writeParam.setCompressionType(writeParam.getCompressionTypes()[WebPWriteParam.LOSSY_COMPRESSION]);
-                        //设置 80% 的质量. 设置范围 0-1
-                        writeParam.setCompressionQuality(0.8f);
-                        // Save the image
-                        ios =ImageIO.createImageOutputStream(newFile);
-                        writer.setOutput(ios);
-                        writer.write(null, new IIOImage(image, null, null), writeParam);
-                        writer.dispose();
-                        ios.close();
-                        minioClient.putObject(PutObjectArgs.builder()
-                                .bucket(minioConfig.getBucket())
-                                .object(completePath)
-                                .stream(Files.newInputStream(newFile.toPath()), newFile.length(), -1)
-                                .build());
-                        inputStream.close();
-                        delteTempFile(newFile);
-                        String src = commonRepository.findOne(72).getValue() + "/" + completePath;
-                        return Result.success().put("data", src);
-                    } catch (IOException e) {
-                        throw new RuntimeException(e);
-                    }finally {
-                        try {
-                            if(inputStream!=null){
-                                inputStream.close();
-                            }
-                            if(ios!=null){
-                                ios.close();
-                            }
-                        } catch (IOException e) {
-                            log.info(e.getMessage());
-                        }
-                    }
-                }
+                //图片压缩webp
+//                InputStream inputStream=file.getInputStream();
+//                if(this.isPicture(suffix)&&file.getSize()>1024*1024*2){
+//                    completePath=getPath(".webp");
+//                    String tmpPath =UUID.randomUUID().toString().replaceAll("-", "")+".webp";
+//                    ApplicationHome applicationHome = new ApplicationHome(AliFileUploadController.class);
+//                    String pathResouce = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath();
+//                    String decode = URLDecoder.decode(pathResouce, "utf-8");
+//                    File newFile = new File(decode + tmpPath);
+//                    ImageOutputStream  ios=null;
+//                    try {
+//                        BufferedImage image = ImageIO.read(inputStream);
+//                        ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();
+//                        WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
+//                        writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
+//                        // 设置有损压缩
+//                        writeParam.setCompressionType(writeParam.getCompressionTypes()[WebPWriteParam.LOSSY_COMPRESSION]);
+//                        //设置 80% 的质量. 设置范围 0-1
+//                        writeParam.setCompressionQuality(0.8f);
+//                        // Save the image
+//                        ios =ImageIO.createImageOutputStream(newFile);
+//                        writer.setOutput(ios);
+//                        writer.write(null, new IIOImage(image, null, null), writeParam);
+//                        writer.dispose();
+//                        ios.close();
+//                        minioClient.putObject(PutObjectArgs.builder()
+//                                .bucket(minioConfig.getBucket())
+//                                .object(completePath)
+//                                .stream(Files.newInputStream(newFile.toPath()), newFile.length(), -1)
+//                                .build());
+//                        inputStream.close();
+//                        delteTempFile(newFile);
+//                        String src = commonRepository.findOne(72).getValue() + "/" + completePath;
+//                        return Result.success().put("data", src);
+//                    } catch (IOException e) {
+//                        throw new RuntimeException(e);
+//                    }finally {
+//                        try {
+//                            if(inputStream!=null){
+//                                inputStream.close();
+//                            }
+//                            if(ios!=null){
+//                                ios.close();
+//                            }
+//                        } catch (IOException e) {
+//                            log.info(e.getMessage());
+//                        }
+//                    }
+//                }
                 minioClient.putObject(PutObjectArgs.builder()
                         .bucket(minioConfig.getBucket())
                         .object(completePath)
-                        .stream(inputStream, fileSize, -1)
+                        .stream(file.getInputStream(), file.getSize(), -1)
                         .build());
 
                 String src = commonRepository.findOne(72).getValue() + "/" + completePath;