1、对接海葵云、EP接口

This commit is contained in:
2025-12-22 10:14:25 +08:00
parent bf26f136c0
commit d830d5dc98
27 changed files with 1011 additions and 4 deletions

View File

@@ -11,6 +11,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Vector;
@@ -409,6 +410,135 @@ public class FilesUtil {
}
}
/**
* 获取指定目录下的所有文件(不递归子目录)
* @param dirPath 目录路径
* @return 目录下所有文件的List集合
* @throws IllegalArgumentException 目录路径不合法时抛出
*/
public static List<File> getAllFiles(String dirPath) {
return getAllFiles(dirPath, false);
}
/**
* 获取指定目录下的所有文件
* @param dirPath 目录路径
* @param recursive 是否递归遍历子目录
* @return 目录下所有文件的List集合
* @throws IllegalArgumentException 目录路径不合法时抛出
*/
public static List<File> getAllFiles(String dirPath, boolean recursive) {
// 1. 校验目录路径合法性
File directory = validateDirectory(dirPath);
// 2. 初始化文件集合
List<File> fileList = new ArrayList<>();
// 3. 遍历目录收集文件
collectFiles(directory, fileList, recursive);
return fileList;
}
/**
* 校验目录合法性
* @param dirPath 目录路径
* @return 合法的目录File对象
* @throws IllegalArgumentException 目录不合法时抛出异常
*/
private static File validateDirectory(String dirPath) {
if (dirPath == null || dirPath.trim().isEmpty()) {
throw new IllegalArgumentException("目录路径不能为空");
}
File directory = new File(dirPath);
if (!directory.exists()) {
throw new IllegalArgumentException("目录不存在: " + dirPath);
}
if (!directory.isDirectory()) {
throw new IllegalArgumentException("指定路径不是目录: " + dirPath);
}
if (!directory.canRead()) {
throw new IllegalArgumentException("没有目录读取权限: " + dirPath);
}
return directory;
}
/**
* 递归收集目录下的文件
* @param directory 目录
* @param fileList 文件集合
* @param recursive 是否递归
*/
private static void collectFiles(File directory, List<File> fileList, boolean recursive) {
// 获取目录下的所有文件/子目录
File[] files = directory.listFiles();
// 空目录直接返回
if (files == null || files.length == 0) {
return;
}
for (File file : files) {
if (file.isFile()) {
// 是文件则加入集合
fileList.add(file);
} else if (file.isDirectory() && recursive) {
// 是目录且需要递归则继续遍历
collectFiles(file, fileList, recursive);
}
}
}
/**
* 非递归删除:仅删除指定文件夹下的一级文件/空文件夹,最后删除文件夹本身
* @param folderPath 目标文件夹路径
* @throws IOException 文件夹不存在、非文件夹、删除失败时抛出异常
*/
public static void deleteFolderNonRecursive(String folderPath) throws IOException {
// 1. 校验文件夹合法性
File targetFolder = new File(folderPath);
if (!targetFolder.exists()) {
throw new IOException("文件夹不存在: " + folderPath);
}
if (!targetFolder.isDirectory()) {
throw new IOException("指定路径不是文件夹: " + folderPath);
}
// 2. 非递归遍历一级内容
File[] files = targetFolder.listFiles();
if (files != null) { // 处理权限问题导致listFiles返回null的情况
for (File file : files) {
if (file.isFile()) {
// 删除一级文件
boolean deleted = file.delete();
if (deleted) {
log.info("已删除文件:{}" , file.getAbsolutePath());
} else {
log.error("删除文件失败:{}" , file.getAbsolutePath());
}
} else if (file.isDirectory()) {
// 仅删除空的一级子文件夹,非空则跳过
if (file.listFiles() == null || file.listFiles().length == 0) {
boolean deleted = file.delete();
if (deleted) {
log.info("已删除空文件夹: {}" , file.getAbsolutePath());
} else {
log.error("删除空文件夹失败: {}" , file.getAbsolutePath());
}
} else {
log.info("跳过非空子文件夹: {}" , file.getAbsolutePath());
}
}
}
}
// 3. 检查目标文件夹是否为空,为空则删除本身
File[] remainingFiles = targetFolder.listFiles();
if (remainingFiles == null || remainingFiles.length == 0) {
boolean folderDeleted = targetFolder.delete();
if (folderDeleted) {
log.info("已删除目标文件夹本身: {}" , targetFolder.getAbsolutePath());
} else {
throw new IOException("删除目标文件夹失败,可能是权限不足: " + folderPath);
}
} else {
throw new IOException("目标文件夹仍有非空内容,无法删除本身: " + folderPath);
}
}
}