Selaa lähdekoodia

更新 上传文件名唯一性

wuweihao 3 vuotta sitten
vanhempi
commit
0a171cbfc6

+ 56 - 0
gis_common/src/main/java/com/gis/common/util/DateUtils.java

@@ -0,0 +1,56 @@
+package com.gis.common.util;
+
+import cn.hutool.core.date.DateUtil;
+import org.junit.Test;
+
+import java.time.LocalDateTime;
+
+/**
+ * Created by owen on 2022/4/8 0008 14:32
+ */
+public class DateUtils extends DateUtil {
+
+    private static String YYYY_MM = "yyyy-MM";
+
+
+    private static String YYYYMM = "yyyyMM";
+
+    private static String YYYY_MM_DD = "yyyy-MM-dd";
+
+    private static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
+
+    private static String YYYYMMDD_HHMMSSSSS = "yyyyMMdd_HHmmssSSS";
+
+    private static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
+
+
+    /**
+     * 获取当前月
+     * @return
+     */
+    public static String getMonth(){
+        return format(LocalDateTime.now(), YYYYMM);
+    }
+
+    /**
+     * 获取当前月份的前几个月
+     * @param month
+     * @return
+     */
+    public static String minusMonths(int month){
+        return format(LocalDateTime.now().minusMonths(month), YYYYMM);
+    }
+
+    /**
+     * 获取当前时间戳
+     * @return
+     */
+    public static String getDateTime(){
+        return format(LocalDateTime.now(), YYYYMMDD_HHMMSSSSS);
+    }
+
+    @Test
+    public void test(){
+        System.out.println(getDateTime());
+    }
+}

+ 5 - 6
gis_common/src/main/java/com/gis/common/util/FileUtils.java

@@ -1,6 +1,5 @@
 package com.gis.common.util;
 
-import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.io.FileUtil;
 import com.gis.common.base.exception.BaseRuntimeException;
 import com.gis.common.constant.ConfigConstant;
@@ -10,9 +9,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,9 +24,11 @@ public class FileUtils {
     @Autowired
     ConfigConstant configConstant;
 
+    // 确保同一时间上传文件的唯一性
+    private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();
+
     public boolean checkFile(MultipartFile file) {
         //设置允许上传文件类型
-//        String suffixList = ".jpg,.gif,.png,.ico,.bmp,.jpeg,.zip,.zp,.rar,.mp3,.mp4,.avi,.mov,.4dage,.wav,.wma,.m4a";
         String suffixList = configConstant.serverFileFallow;
         // 获取文件后缀
         if(file == null){
@@ -70,7 +71,6 @@ public class FileUtils {
         HashMap<String, Object> result = new HashMap<>();
         result.put("fileName", file.getOriginalFilename());
         result.put("filePath", savePath + "/" + newName);
-//        result.put("newName", newName);
         return result;
 
     }
@@ -87,7 +87,6 @@ public class FileUtils {
         // 检查非法文件上传
         boolean checkFile = this.checkFile(file);
         if (!checkFile) {
-//            throw new BaseRuntimeException(3009, "上传文件格式有误, 请重新上传");
             throw new BaseRuntimeException("上传文件格式有误, 请重新上传");
         }
 
@@ -99,7 +98,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;

+ 0 - 50
gis_scene/src/main/java/com/gis/scene/controller/SceneController.java

@@ -109,55 +109,5 @@ public class SceneController  {
     }
 
 
-//    @ApiOperation(value = "上传到指定场景码目录", notes = "时间戳命名")
-//    @PostMapping(value = "upload/{sceneCode}", consumes = {"multipart/form-data"})
-//    public Result upload(MultipartFile file , @PathVariable String sceneCode) throws IOException {
-//
-//        if (file == null) {
-//            log.error("文件不能为空");
-//            return Result.failure("文件不能为空");
-//        }
-//
-//        SceneEntity entity = sceneService.findBySceneCode(sceneCode);
-//        if (entity == null) {
-//            log.error("场景不存在: {}", sceneCode);
-//            return Result.failure("场景不存在");
-//        }
-//
-//        String fileName = file.getOriginalFilename();
-//        log.info("原始文件名: {}", fileName);
-////        String time = DateUtil.format(LocalDateTime.now(), "yyyyMMdd_HHmmssSSS");
-////        String suffix = StringUtils.substringAfterLast(fileName, ".");
-////        suffix = StringUtils.lowerCase(suffix);
-//
-////        String newName = time  + "."  +suffix;
-//
-//
-//
-//
-//        // 去除特殊符号
-//        String pinyinName = RegexUtil.specificSymbol(fileName);
-//        pinyinName = PinyinUtil.getPinyin(pinyinName, "");
-//
-//        // 转小写
-//        pinyinName =  StringUtils.lowerCase(pinyinName);
-//
-//
-//
-//        String basePath = configConstant.serverBasePath + sceneCode;
-//        String savePath = basePath + "/edit/" + pinyinName;
-//        log.info("文件保存位置:" + savePath);
-//        FileUtil.writeFromStream(file.getInputStream(), savePath);
-//
-//
-//        Object urlPath =  entity.getPath() + "/edit/" + pinyinName;
-//
-//        log.info("文件写入成功: {}", urlPath);
-//
-//        // 返回前端数据
-//        return Result.success(urlPath);
-//
-//    }
-
 
 }