|
@@ -1,6 +1,7 @@
|
|
package com.gis.common.util;
|
|
package com.gis.common.util;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
+import cn.hutool.core.img.ImgUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import com.gis.common.base.exception.BaseRuntimeException;
|
|
import com.gis.common.base.exception.BaseRuntimeException;
|
|
import com.gis.common.constant.ConfigConstant;
|
|
import com.gis.common.constant.ConfigConstant;
|
|
@@ -10,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
+import java.io.File;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
@@ -31,17 +33,18 @@ public class FileUtils {
|
|
String suffixList = configConstant.serverFileFallow;
|
|
String suffixList = configConstant.serverFileFallow;
|
|
// 获取文件后缀
|
|
// 获取文件后缀
|
|
if(file == null){
|
|
if(file == null){
|
|
- log.info("文件流为空不可上传");
|
|
|
|
|
|
+ log.error("文件流为空不可上传");
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
String fileName = file.getOriginalFilename();
|
|
String fileName = file.getOriginalFilename();
|
|
|
|
+ log.info("文件名: {}", fileName);
|
|
String suffix = fileName.substring(fileName.lastIndexOf(".")
|
|
String suffix = fileName.substring(fileName.lastIndexOf(".")
|
|
+ 1, fileName.length());
|
|
+ 1, fileName.length());
|
|
if (suffixList.contains(suffix.trim().toLowerCase())) {
|
|
if (suffixList.contains(suffix.trim().toLowerCase())) {
|
|
- log.info("无非法参数可以放行!!!");
|
|
|
|
|
|
+// log.info("无非法参数可以放行!!!");
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
- log.info("存在非法参数不能放行!请核对上传文件格式,重新刷新页面再次上传!");
|
|
|
|
|
|
+ log.error("存在非法参数不能放行!请核对上传文件格式,重新刷新页面再次上传!");
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -76,6 +79,62 @@ public class FileUtils {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 上传文件且按比例压缩
|
|
|
|
+ * @param file
|
|
|
|
+ * @param savePath
|
|
|
|
+ * @param isPinYinRename
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public Map<String, Object> imgUploadMapAndCompress(MultipartFile file, String savePath, boolean isPinYinRename) {
|
|
|
|
+ String newName = this.upload(file, savePath, isPinYinRename);
|
|
|
|
+ String filePath = savePath + "/" + newName;
|
|
|
|
+ long size = file.getSize();
|
|
|
|
+ size = size/1000;
|
|
|
|
+ log.info("文件大小:" + size + "kb");
|
|
|
|
+ if (size > 1024){
|
|
|
|
+ String inPath = configConstant.serverBasePath + savePath + "/" + newName;
|
|
|
|
+ String outPath = configConstant.serverBasePath + savePath + "/cp_" + newName;
|
|
|
|
+
|
|
|
|
+// ImgUtil.compress(new File(inPath), new File(outPath), 0.5f);
|
|
|
|
+ ImgUtil.compress(new File(inPath), new File(outPath), getQuality(size));
|
|
|
|
+ log.info("压缩图片位置: {}", outPath);
|
|
|
|
+ filePath = savePath + "/cp_" + newName;
|
|
|
|
+ this.del(inPath);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ HashMap<String, Object> result = new HashMap<>();
|
|
|
|
+ result.put("fileName", file.getOriginalFilename());
|
|
|
|
+ result.put("filePath", filePath);
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 通过图片大小计算压缩比例
|
|
|
|
+ * @param size 单位是KB
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private float getQuality(long size){
|
|
|
|
+ // 单位用m
|
|
|
|
+ double n = size/1024;
|
|
|
|
+
|
|
|
|
+ // 压缩比例,2M以内使用0.5压缩
|
|
|
|
+ double quality = 0.5;
|
|
|
|
+ // 大于2M 按计算比例压缩
|
|
|
|
+ if (n > 2) {
|
|
|
|
+ quality = 1 / n;
|
|
|
|
+ }
|
|
|
|
+ log.info("压缩比例: {}", quality);
|
|
|
|
+ Float aFloat = Float.valueOf(quality + "");
|
|
|
|
+ return aFloat;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
/**
|
|
/**
|
|
*
|
|
*
|
|
@@ -89,9 +148,11 @@ public class FileUtils {
|
|
// 检查非法文件上传
|
|
// 检查非法文件上传
|
|
boolean checkFile = this.checkFile(file);
|
|
boolean checkFile = this.checkFile(file);
|
|
if (!checkFile) {
|
|
if (!checkFile) {
|
|
|
|
+// throw new BaseRuntimeException(3009, "上传文件格式有误, 请重新上传");
|
|
throw new BaseRuntimeException("上传文件格式有误, 请重新上传");
|
|
throw new BaseRuntimeException("上传文件格式有误, 请重新上传");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
// 文件目录
|
|
// 文件目录
|
|
String fileName = file.getOriginalFilename();
|
|
String fileName = file.getOriginalFilename();
|
|
String newName;
|
|
String newName;
|