|
|
@@ -1,7 +1,6 @@
|
|
|
package com.kede.common.controller;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
@@ -9,6 +8,7 @@ import java.util.List;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
+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;
|
|
|
@@ -32,7 +32,7 @@ public class BaseApiController extends BaseController {
|
|
|
private String uploadPrivatePath = null;
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ *
|
|
|
* @param request
|
|
|
* @return
|
|
|
*/
|
|
|
@@ -49,7 +49,7 @@ public class BaseApiController extends BaseController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ *
|
|
|
* @param res
|
|
|
* @param path
|
|
|
* @param isPrivate
|
|
|
@@ -64,7 +64,13 @@ public class BaseApiController extends BaseController {
|
|
|
try {
|
|
|
MultipartFile mf = files.get(0);
|
|
|
fileName = FileUtils.splicingFileName(mf, isPrivate, sign, path, TokenManager.getAdminId());
|
|
|
- mf.transferTo(isPrivate ? toPrivateFile(fileName) : toFile(fileName));
|
|
|
+ if (mf.getSize()<=1024*1024*3){
|
|
|
+ File f = isPrivate ? toPrivateFile(fileName) : toFile(fileName);
|
|
|
+ mf.transferTo(f);
|
|
|
+ }else {
|
|
|
+ //主要操作,先将文件的长宽变小,再进行压缩
|
|
|
+ Thumbnails.of(mf.getInputStream()).scale(0.25f).toFile(uploadPath+fileName);
|
|
|
+ }
|
|
|
LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
|
|
|
} catch (Exception e) {
|
|
|
LoggerUtils.error(getClass(), "文件上传失败", e);
|
|
|
@@ -75,15 +81,13 @@ public class BaseApiController extends BaseController {
|
|
|
}
|
|
|
return fileName;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ *
|
|
|
* @param res
|
|
|
- * @param path
|
|
|
* @param isPrivate
|
|
|
* @param req
|
|
|
- * @param sign
|
|
|
* @return
|
|
|
*/
|
|
|
protected String handleFile(Result res, boolean isPrivate, HttpServletRequest req, String... fileName) {
|
|
|
@@ -94,7 +98,12 @@ public class BaseApiController extends BaseController {
|
|
|
MultipartFile mf = files.get(0);
|
|
|
realFilePath = FileUtils.buildFilePath(fileName) + FileUtils.getSuffix(mf.getOriginalFilename());
|
|
|
File f = isPrivate ? toPrivateFile(realFilePath) : toFile(realFilePath);
|
|
|
- mf.transferTo(f);
|
|
|
+ if (mf.getSize()<=1024*20){
|
|
|
+ mf.transferTo(f);
|
|
|
+ }else {
|
|
|
+ //主要操作,先将文件的长宽变小,再进行压缩
|
|
|
+ Thumbnails.of(mf.getInputStream()).size(500,500).outputQuality(0.25d).toFile(f);
|
|
|
+ }
|
|
|
LoggerUtils.debug(getClass(), realFilePath + " 文件上传成功");
|
|
|
} catch (Exception e) {
|
|
|
LoggerUtils.error(getClass(), "文件上传失败", e);
|
|
|
@@ -107,7 +116,7 @@ public class BaseApiController extends BaseController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ *
|
|
|
* @param res
|
|
|
* @param fileName
|
|
|
* @param filePath
|
|
|
@@ -127,7 +136,7 @@ public class BaseApiController extends BaseController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ *
|
|
|
* @param fileName
|
|
|
* @return
|
|
|
*/
|
|
|
@@ -136,7 +145,7 @@ public class BaseApiController extends BaseController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ *
|
|
|
* @param fileName
|
|
|
* @return
|
|
|
*/
|
|
|
@@ -145,7 +154,7 @@ public class BaseApiController extends BaseController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ *
|
|
|
* @param fileName
|
|
|
* @return
|
|
|
*/
|