|
@@ -4,8 +4,11 @@ import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.img.Img;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.thread.ThreadUtil;
|
|
|
+import cn.hutool.core.util.ReUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.gis.common.constant.ConfigConstant;
|
|
|
+import com.gis.common.exception.BaseRuntimeException;
|
|
|
+import com.gis.common.util.DateUtils;
|
|
|
import com.gis.common.util.ImageUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -23,7 +26,9 @@ import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLDecoder;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
/**
|
|
|
* Created by owen on 2020/5/12 0012 17:21
|
|
@@ -39,6 +44,69 @@ public class FileUtils {
|
|
|
@Autowired
|
|
|
ConfigConstant configConstant;
|
|
|
|
|
|
+ // 确保同一时间上传文件的唯一性
|
|
|
+ private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param isPinYinRename false:时间戳重命名, true:用拼音重名文件
|
|
|
+ * @param savePath 保存地址(前面有斜杠, 后面没有),没有文件名
|
|
|
+ * @return map
|
|
|
+ */
|
|
|
+ public Map<String, Object> uploadMap(MultipartFile file, String savePath, boolean isPinYinRename) {
|
|
|
+ String newName = this.upload(file, savePath, isPinYinRename);
|
|
|
+ HashMap<String, Object> result = new HashMap<>();
|
|
|
+ result.put("fileName", file.getOriginalFilename());
|
|
|
+ result.put("filePath", savePath + "/" + newName);
|
|
|
+ return result;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param isPinYinRename false:时间戳重命名, true:用拼音重名文件
|
|
|
+ * @param savePath 保存地址(前面有斜杠, 后面没有),没有文件名
|
|
|
+ * @return 文件名
|
|
|
+ */
|
|
|
+ public String upload(MultipartFile file, String savePath, boolean isPinYinRename) {
|
|
|
+
|
|
|
+ // 检查非法文件上传
|
|
|
+ boolean checkFile = this.checkFile(file);
|
|
|
+ if (!checkFile) {
|
|
|
+ throw new BaseRuntimeException("上传文件格式有误, 请重新上传");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 文件目录
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ String newName;
|
|
|
+ if (isPinYinRename){
|
|
|
+ newName = RegexUtil.getPinyinName(fileName);
|
|
|
+ } else {
|
|
|
+ String suffix = StringUtils.substringAfterLast(fileName, ".");
|
|
|
+ newName = this.getDateTime() + ATOMIC_INTEGER.incrementAndGet() + "." + suffix;
|
|
|
+ }
|
|
|
+
|
|
|
+ savePath = configConstant.serverBasePath + savePath + "/" + newName;
|
|
|
+ log.info("保存文件地址:{}", savePath);
|
|
|
+
|
|
|
+ try {
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
+
|
|
|
+ return newName;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static String getDateTime(){
|
|
|
+ return DateUtil.format(LocalDateTime.now(), "YYYYMMDD_HHMMSSSSS");
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 多文件上传
|
|
@@ -761,14 +829,36 @@ public class FileUtils {
|
|
|
* 真删除文件
|
|
|
* @param path 参数是相对地址
|
|
|
*/
|
|
|
+// public void del(String path){
|
|
|
+// if (StrUtil.isNotBlank(path)){
|
|
|
+// String delPath = configConstant.serverBasePath + path;
|
|
|
+// FileUtil.del(delPath);
|
|
|
+// log.info("真删除文件: {}", delPath);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 真删除文件
|
|
|
+ * @param path 参数是相对地址
|
|
|
+ */
|
|
|
public void del(String path){
|
|
|
- if (StrUtil.isNotBlank(path)){
|
|
|
- String delPath = configConstant.serverBasePath + path;
|
|
|
- FileUtil.del(delPath);
|
|
|
- log.info("真删除文件: {}", delPath);
|
|
|
+ if (StrUtil.isBlank(path)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.info("path: {}", path);
|
|
|
+ // 过滤 路径为/////, 后面必须跟非斜杠字符
|
|
|
+ String regex = "^[/|\\\\]{1,2}[^/|\\\\]\\S";
|
|
|
+ if (!ReUtil.contains(regex, path)){
|
|
|
+ log.warn("非法路径删除:{}", path);
|
|
|
+ return;
|
|
|
}
|
|
|
+
|
|
|
+ String delPath = configConstant.serverBasePath + path;
|
|
|
+ FileUtil.del(delPath);
|
|
|
+ log.info("真删除文件: {}", delPath);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
}
|