|
@@ -1,7 +1,7 @@
|
|
|
package com.gis.common.util;
|
|
|
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.gis.common.config.ConfigConstant;
|
|
|
import com.gis.common.exception.BaseRuntimeException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -10,9 +10,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -25,6 +25,9 @@ public class FileUtils {
|
|
|
@Autowired
|
|
|
ConfigConstant configConstant;
|
|
|
|
|
|
+ // 确保同一时间上传文件的唯一性
|
|
|
+ private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();
|
|
|
+
|
|
|
public boolean checkFile(MultipartFile file) {
|
|
|
//设置允许上传文件类型
|
|
|
String suffixList = configConstant.serverFileFallow;
|
|
@@ -67,7 +70,7 @@ public class FileUtils {
|
|
|
newName = RegexUtil.getPinyinName(fileName);
|
|
|
} else {
|
|
|
String suffix = StringUtils.substringAfterLast(fileName, ".");
|
|
|
- newName = DateUtil.format(LocalDateTime.now(), "yyyyMMdd_HHmmssSSS") + "." + suffix;
|
|
|
+ newName = DateUtils.getDateTime() + ATOMIC_INTEGER.incrementAndGet() + "." + suffix;
|
|
|
}
|
|
|
|
|
|
savePath = configConstant.serverBasePath + savePath + "/" + newName;
|
|
@@ -75,7 +78,6 @@ public class FileUtils {
|
|
|
|
|
|
try {
|
|
|
FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
-
|
|
|
return newName;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
@@ -104,6 +106,9 @@ public class FileUtils {
|
|
|
* @param path 参数是相对地址
|
|
|
*/
|
|
|
public void del(String path){
|
|
|
+ if (StrUtil.isBlank(path)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
String delPath = configConstant.serverBasePath + path;
|
|
|
FileUtil.del(delPath);
|
|
|
log.info("真删除文件: {}", delPath);
|