拆分查询详情接口变成查询列表,以及单个查询,防止文件过多过大造成请求超时

This commit is contained in:
2025-08-01 14:20:26 +08:00
parent 0e6fed8562
commit 99f2d8e970
3 changed files with 105 additions and 26 deletions

View File

@@ -1,7 +1,8 @@
package com.klp.controller;
import com.klp.domain.vo.ApiResponseVo;
import com.klp.domain.FileDetailInfo;
import com.klp.domain.FileInfo;
import com.klp.domain.vo.ApiResponseVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
@@ -13,9 +14,10 @@ import java.nio.file.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
/**
* 文件管理控制器
* WMS文件管理控制器
* 提供文件读取、删除和定时删除功能
*/
@Controller
@@ -26,8 +28,8 @@ public class WmsFileManagementController {
private static final String DIRECTORY_PATH = "testDirectory";
/**
* 获取目录下所有文件信息
* GET /api/file/list
* 获取目录下所有文件基本信息(不包含文件内容)
* GET /wms/file/list
*/
@GetMapping("/list")
@ResponseBody
@@ -40,12 +42,12 @@ public class WmsFileManagementController {
return ResponseEntity.ok(new ApiResponseVo<>(false, "目录不存在: " + DIRECTORY_PATH, null));
}
// 获取所有文件
// 获取所有文件基本信息(不读取文件内容)
List<FileInfo> fileList = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) {
for (Path file : stream) {
if (Files.isRegularFile(file)) {
FileInfo fileInfo = readFileInfo(file);
FileInfo fileInfo = readFileBasicInfo(file);
fileList.add(fileInfo);
}
}
@@ -63,9 +65,44 @@ public class WmsFileManagementController {
}
}
/**
* 获取单个文件的详细信息(包含文件内容)
* GET /wms/file/content/{fileName}
*/
@GetMapping("/content/{fileName}")
@ResponseBody
public ResponseEntity<ApiResponseVo<FileDetailInfo>> getFileContent(@PathVariable String fileName) {
try {
Path filePath = Paths.get(DIRECTORY_PATH, fileName);
// 检查文件是否存在
if (!Files.exists(filePath)) {
return ResponseEntity.ok(new ApiResponseVo<>(false, "文件不存在: " + fileName, null));
}
// 检查是否为普通文件
if (!Files.isRegularFile(filePath)) {
return ResponseEntity.ok(new ApiResponseVo<>(false, "不是普通文件: " + fileName, null));
}
// 读取文件详细信息(包含内容)
FileDetailInfo fileDetailInfo = readFileDetailInfo(filePath);
String message = "成功获取文件内容: " + fileName;
return ResponseEntity.ok(new ApiResponseVo<>(true, message, fileDetailInfo));
} catch (Exception e) {
String errorMsg = "获取文件内容失败: " + e.getMessage();
System.err.println(errorMsg);
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ApiResponseVo<>(false, errorMsg, null));
}
}
/**
* 根据文件名删除文件
* DELETE /api/file/delete/{fileName}
* DELETE /wms/file/delete/{fileName}
*/
@DeleteMapping("/delete/{fileName}")
@ResponseBody
@@ -100,7 +137,7 @@ public class WmsFileManagementController {
/**
* 批量删除文件
* POST /api/file/batch-delete
* POST /wms/file/batch-delete
*/
@PostMapping("/batch-delete")
@ResponseBody
@@ -145,8 +182,8 @@ public class WmsFileManagementController {
}
/**
* 手动执行定时删除删除3个月的文件)
* POST /api/file/cleanup
* 手动执行定时删除(删除3个月的文件
* POST /wms/file/cleanup
*/
@PostMapping("/cleanup")
@ResponseBody
@@ -156,7 +193,7 @@ public class WmsFileManagementController {
/**
* 定时删除任务 - 每3个月执行一次
* 删除3个月前的文件
* 删除前3个月的文件1-3个月前的文件
*/
@Scheduled(cron = "0 0 2 1 */3 *") // 每3个月的第1天凌晨2点执行
public void scheduledCleanup() {
@@ -189,7 +226,8 @@ public class WmsFileManagementController {
return ResponseEntity.ok(new ApiResponseVo<>(false, "目录不存在: " + DIRECTORY_PATH, null));
}
// 计算3个月前的日期
// 计算时间范围删除1-3个月前的文件
LocalDateTime oneMonthAgo = LocalDateTime.now().minusMonths(1);
LocalDateTime threeMonthsAgo = LocalDateTime.now().minusMonths(3);
List<String> deletedFiles = new ArrayList<>();
@@ -205,8 +243,8 @@ public class WmsFileManagementController {
java.time.ZoneId.systemDefault()
);
// 如果文件修改时间早于3个月前则删除
if (fileModifiedTime.isBefore(threeMonthsAgo)) {
// 如果文件修改时间在1-3个月前之间,则删除
if (fileModifiedTime.isBefore(oneMonthAgo) && fileModifiedTime.isAfter(threeMonthsAgo)) {
Files.delete(file);
deletedFiles.add(file.getFileName().toString());
deletedCount++;
@@ -222,9 +260,10 @@ public class WmsFileManagementController {
result.put("deletedCount", deletedCount);
result.put("deletedFiles", deletedFiles);
result.put("cleanupTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
result.put("thresholdTime", threeMonthsAgo.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
result.put("startTime", oneMonthAgo.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
result.put("endTime", threeMonthsAgo.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
String message = String.format("清理完成: 删除了 %d 个3个月的文件", deletedCount);
String message = String.format("清理完成: 删除了 %d 个3个月的文件", deletedCount);
return ResponseEntity.ok(new ApiResponseVo<>(true, message, result));
} catch (Exception e) {
@@ -237,9 +276,25 @@ public class WmsFileManagementController {
}
/**
* 读取文件信息
* 读取文件基本信息(不包含文件内容)
*/
private FileInfo readFileInfo(Path filePath) throws IOException {
private FileInfo readFileBasicInfo(Path filePath) throws IOException {
String fileName = filePath.getFileName().toString();
long fileSize = Files.size(filePath);
LocalDateTime lastModified = LocalDateTime.ofInstant(
Files.getLastModifiedTime(filePath).toInstant(),
java.time.ZoneId.systemDefault()
);
String lastModifiedStr = lastModified.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return new FileInfo(fileName, fileSize, lastModifiedStr, filePath.toString());
}
/**
* 读取文件详细信息(包含文件内容)
*/
private FileDetailInfo readFileDetailInfo(Path filePath) throws IOException {
String fileName = filePath.getFileName().toString();
String fileContent = readFileContent(filePath);
long fileSize = Files.size(filePath);
@@ -250,7 +305,7 @@ public class WmsFileManagementController {
);
String lastModifiedStr = lastModified.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return new FileInfo(fileName, fileContent, fileSize, lastModifiedStr, filePath.toString());
return new FileDetailInfo(fileName, fileContent, fileSize, lastModifiedStr, filePath.toString());
}
/**
@@ -272,7 +327,7 @@ public class WmsFileManagementController {
/**
* 获取目录统计信息
* GET /api/file/stats
* GET /wms/file/stats
*/
@GetMapping("/stats")
@ResponseBody

View File

@@ -0,0 +1,26 @@
package com.klp.domain;
import lombok.Data;
/**
* 文件详细信息实体类(包含文件内容)
*/
@Data
public class FileDetailInfo {
private String fileName;
private String fileContent;
private long fileSize;
private String lastModified;
private String filePath;
public FileDetailInfo() {
}
public FileDetailInfo(String fileName, String fileContent, long fileSize, String lastModified, String filePath) {
this.fileName = fileName;
this.fileContent = fileContent;
this.fileSize = fileSize;
this.lastModified = lastModified;
this.filePath = filePath;
}
}

View File

@@ -1,24 +1,22 @@
package com.klp.domain;
import jdk.jfr.DataAmount;
import lombok.Data;
/**
* 文件信息实体类
* 文件基本信息实体类(用于列表展示)
*/
@Data
public class FileInfo {
private String fileName;
private String fileContent;
private long fileSize;
private String lastModified;
private String filePath;
public FileInfo() {
}
public FileInfo() {}
public FileInfo(String fileName, String fileContent, long fileSize, String lastModified, String filePath) {
public FileInfo(String fileName, long fileSize, String lastModified, String filePath) {
this.fileName = fileName;
this.fileContent = fileContent;
this.fileSize = fileSize;
this.lastModified = lastModified;
this.filePath = filePath;