|
|
@@ -1,15 +1,11 @@
|
|
|
package com.goafanti.common.controller;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
+import com.goafanti.common.bo.Result;
|
|
|
+import com.goafanti.common.utils.FileUtils;
|
|
|
+import com.goafanti.common.utils.LoggerUtils;
|
|
|
+import com.goafanti.core.shiro.token.TokenManager;
|
|
|
import com.goafanti.order.enums.OrderImgEnums;
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
@@ -18,10 +14,13 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import org.springframework.web.multipart.MultipartRequest;
|
|
|
|
|
|
-import com.goafanti.common.bo.Result;
|
|
|
-import com.goafanti.common.utils.FileUtils;
|
|
|
-import com.goafanti.common.utils.LoggerUtils;
|
|
|
-import com.goafanti.core.shiro.token.TokenManager;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping(value = "/api")
|
|
|
@@ -85,7 +84,12 @@ public class BaseApiController extends BaseController {
|
|
|
try {
|
|
|
MultipartFile mf = files.get(0);
|
|
|
fileName = FileUtils.orderFileName(mf,id,path,sign);
|
|
|
- mf.transferTo(toFile(fileName));
|
|
|
+ File file=toFile(fileName);
|
|
|
+
|
|
|
+ mf.transferTo(file);
|
|
|
+ // 图片尺寸不变,压缩图片文件大小outputQuality实现,参数1为最高质量
|
|
|
+ //拼接文件路劲
|
|
|
+ fileName=compressFile(file,fileName);
|
|
|
LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
|
|
|
} catch (Exception e) {
|
|
|
LoggerUtils.error(getClass(), "文件上传失败", e);
|
|
|
@@ -97,6 +101,27 @@ public class BaseApiController extends BaseController {
|
|
|
return fileName;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 图片压缩
|
|
|
+ * @param file
|
|
|
+ * @param fileName
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private String compressFile(File file, String fileName) throws IOException {
|
|
|
+ if (fileName.contains("jpg")||fileName.contains("jpeg")||fileName.contains("bmp")
|
|
|
+ ||fileName.contains("png")){
|
|
|
+ String thumbnailFilePathName = fileName.substring(0, fileName.lastIndexOf(".")) + "_min"+fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ double scale = 0.4d ;
|
|
|
+ Thumbnails.of(file).scale(1f).outputQuality(scale)
|
|
|
+ .outputFormat("jpg")
|
|
|
+ .toFile(uploadPath+thumbnailFilePathName);
|
|
|
+ FileUtils.deleteFile(uploadPath+fileName);
|
|
|
+ return thumbnailFilePathName;
|
|
|
+ }
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+
|
|
|
protected Result deleteFile (Result res,String fileName){
|
|
|
if (StringUtils.isBlank(fileName)) {
|
|
|
res.getError().add(buildError("文件路径错误"));
|