فهرست منبع

完成了文件名以及文件类型获取设置

李英俊ya 1 ماه پیش
والد
کامیت
93dd0ba4f5

+ 5 - 2
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRepoController.java

@@ -13,6 +13,7 @@ import com.ruoyi.common.utils.VersionUtils;
 import com.ruoyi.common.utils.OfficeCompareUtils;
 import com.ruoyi.system.service.ISysRepoHistoryService;
 import com.ruoyi.common.utils.file.FileUtils;
+import org.apache.commons.io.FilenameUtils;
 import org.springframework.http.MediaType;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -77,8 +78,9 @@ public class SysRepoController extends BaseController
     {
         sysRepo.setCreateBy(getUsername());
         sysRepo.setUpdateBy(getUsername());
-        String filename = StringUtils.substringAfterLast(sysRepo.getUrl(), "/");
+        String filename = FileUtils.getNameNotExtract(sysRepo.getUrl());
         sysRepo.setFileName(filename);
+        sysRepo.setRepoType(FileUtils.getFileExtension(filename));
         sysRepo.setVersion(VersionUtils.incrementVersion(sysRepo.getVersion()));
         return toAjax(sysRepoService.insertSysRepo(sysRepo));
     }
@@ -96,8 +98,9 @@ public class SysRepoController extends BaseController
         }else{
             SysRepo  sysRepo_old = sysRepoService.selectSysRepoByRepoId(sysRepo.getRepoId());
             sysRepo.setUpdateBy(getUsername());
-            String filename = StringUtils.substringAfterLast(sysRepo.getUrl(), "/");
+            String filename = FileUtils.getNameNotExtract(sysRepo.getUrl());
             sysRepo.setFileName(filename);
+            sysRepo.setRepoType(FileUtils.getFileExtension(filename));
             sysRepo.setVersion(VersionUtils.incrementVersion(sysRepo_old.getVersion()));
             SysRepoHistory sysRepoHistory = new SysRepoHistory();
             sysRepoHistory.setRepoId(sysRepo_old.getRepoId());

+ 41 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java

@@ -288,4 +288,45 @@ public class FileUtils
         String baseName = FilenameUtils.getBaseName(fileName);
         return baseName;
     }
+
+    /**
+     * 获取文件名称 /profile/upload/2025/05/20/test_20250520101754A001.png -- test.png
+     *
+     * @param fileName 路径名称
+     * @return 没有文件路径以及扩展的上传文件名称
+     */
+    public static String getNameNotExtract(String fileName)
+    {
+        if (fileName == null)
+        {
+            return null;
+        }
+        int lastUnixPos = fileName.lastIndexOf('/');
+        int lastWindowsPos = fileName.lastIndexOf('\\');
+        int index = Math.max(lastUnixPos, lastWindowsPos);
+        String  baseName = fileName.substring(index + 1);
+        int lastUnderscoreIndex = baseName.lastIndexOf('_');
+        int lastDotIndex = baseName.lastIndexOf('.');
+        if (lastDotIndex == -1 || lastDotIndex < lastUnderscoreIndex) {
+            return baseName.substring(0, lastUnderscoreIndex);
+        }
+        return baseName.substring(0, lastUnderscoreIndex) + baseName.substring(lastDotIndex);
+    }
+
+    /**
+     * 获取文件名称
+     *
+     * @param fileName 路径名称
+     * @return 文件类型后缀
+     */
+    public static String getFileExtension(String fileName) {
+        if (fileName == null || fileName.isEmpty()) {
+            return null;
+        }
+        int lastDotIndex = fileName.lastIndexOf('.');
+        if (lastDotIndex == -1 || lastDotIndex == 0) {
+            return "";
+        }
+        return fileName.substring(lastDotIndex + 1);
+    }
 }