|
@@ -3,7 +3,6 @@ package com.sqx.modules.file;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.aliyun.oss.OSS;
|
|
import com.aliyun.oss.OSS;
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
-import com.luciad.imageio.webp.WebPWriteParam;
|
|
|
|
|
import com.sqx.common.exception.SqxException;
|
|
import com.sqx.common.exception.SqxException;
|
|
|
import com.sqx.common.utils.Result;
|
|
import com.sqx.common.utils.Result;
|
|
|
import com.sqx.config.MinioConfig;
|
|
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.MultipartFile;
|
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
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.io.*;
|
|
|
import java.net.URLDecoder;
|
|
import java.net.URLDecoder;
|
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
@@ -93,60 +85,61 @@ public class AliFileUploadController {
|
|
|
// Minio上传
|
|
// Minio上传
|
|
|
String suffix = file.getOriginalFilename().substring(Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf("."));
|
|
String suffix = file.getOriginalFilename().substring(Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf("."));
|
|
|
String completePath = getPath(suffix);
|
|
String completePath = getPath(suffix);
|
|
|
- long fileSize =file.getSize();
|
|
|
|
|
|
|
+// long fileSize =file.getSize();
|
|
|
try {
|
|
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()
|
|
minioClient.putObject(PutObjectArgs.builder()
|
|
|
.bucket(minioConfig.getBucket())
|
|
.bucket(minioConfig.getBucket())
|
|
|
.object(completePath)
|
|
.object(completePath)
|
|
|
- .stream(inputStream, fileSize, -1)
|
|
|
|
|
|
|
+ .stream(file.getInputStream(), file.getSize(), -1)
|
|
|
.build());
|
|
.build());
|
|
|
|
|
|
|
|
String src = commonRepository.findOne(72).getValue() + "/" + completePath;
|
|
String src = commonRepository.findOne(72).getValue() + "/" + completePath;
|