diff --git a/common/src/main/java/com/sdm/common/utils/FilesUtil.java b/common/src/main/java/com/sdm/common/utils/FilesUtil.java index 0f44bc7e..bd166762 100644 --- a/common/src/main/java/com/sdm/common/utils/FilesUtil.java +++ b/common/src/main/java/com/sdm/common/utils/FilesUtil.java @@ -818,6 +818,57 @@ public class FilesUtil { } } + /** + * 获取基础路径下所有一级子文件夹的绝对路径 + * + * @param basePath 基础路径,如 /hpc/123/uuid/ 或 D:/hpc/123/uuid/ + * @return 所有子文件夹的绝对路径列表 + */ + public static List getAllSubFolderAbsolutePaths(String basePath) { + List folderPaths = new ArrayList<>(); + // 1. 构建基础路径文件对象 + File baseDir = new File(basePath); + // 2. 校验路径是否存在 & 是否是文件夹 + if (!baseDir.exists()) { + throw new IllegalArgumentException("基础路径不存在:" + basePath); + } + if (!baseDir.isDirectory()) { + throw new IllegalArgumentException("基础路径不是文件夹:" + basePath); + } + // 3. 获取路径下所有文件/文件夹 + File[] files = baseDir.listFiles(); + if (files == null || files.length == 0) { + return folderPaths; // 空文件夹,返回空列表 + } + // 4. 筛选出【文件夹】,并获取绝对路径 + for (File file : files) { + if (file.isDirectory()) { + folderPaths.add(file.getAbsolutePath()); + } + } + return folderPaths; + } + + /** + * 从路径中获取最后一层目录名(兼容所有系统、兼容末尾带 /) + * 示例: + * /hpc/123/uuid -> uuid + * /hpc/123/uuid/ -> uuidd + */ + public static String getLastDirectoryName(String path) { + if (path == null || path.isBlank()) { + return null; + } + + Path filePath = Paths.get(path).normalize(); + Path fileName = filePath.getFileName(); + + return fileName == null ? null : fileName.toString(); + } + + + + }