feat(file-management): 添加文件管理功能并支持环境配置

- 在 application.yml 和环境特定的配置文件中添加文件存储目录路径配置
- 新增 Stage 环境配置文件和相关设置
- 修改 WmsFileManagementController 以使用配置文件中的目录路径
- 优化文件管理相关 API 的错误处理和路径获取方式
This commit is contained in:
2025-08-04 13:11:14 +08:00
parent b8a0792b5d
commit d1255c0312
6 changed files with 217 additions and 12 deletions

View File

@@ -3,6 +3,7 @@ package com.klp.controller;
import com.klp.domain.FileDetailInfo;
import com.klp.domain.FileInfo;
import com.klp.domain.vo.ApiResponseVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
@@ -24,8 +25,9 @@ import java.util.stream.Collectors;
@RequestMapping("/wms/file")
public class WmsFileManagementController {
// 文件目录路径
private static final String DIRECTORY_PATH = "testDirectory";
// 文件目录路径 - 从配置文件读取
@Value("${klp.file.directory-path}")
private String directoryPath;
/**
* 获取目录下所有文件基本信息(不包含文件内容)
@@ -35,11 +37,11 @@ public class WmsFileManagementController {
@ResponseBody
public ResponseEntity<ApiResponseVo<List<FileInfo>>> getAllFiles() {
try {
Path directory = Paths.get(DIRECTORY_PATH);
Path directory = Paths.get(directoryPath);
// 检查目录是否存在
if (!Files.exists(directory)) {
return ResponseEntity.ok(new ApiResponseVo<>(false, "目录不存在: " + DIRECTORY_PATH, null));
return ResponseEntity.ok(new ApiResponseVo<>(false, "目录不存在: " + directoryPath, null));
}
// 获取所有文件基本信息(不读取文件内容)
@@ -73,7 +75,7 @@ public class WmsFileManagementController {
@ResponseBody
public ResponseEntity<ApiResponseVo<FileDetailInfo>> getFileContent(@PathVariable String fileName) {
try {
Path filePath = Paths.get(DIRECTORY_PATH, fileName);
Path filePath = Paths.get(directoryPath, fileName);
// 检查文件是否存在
if (!Files.exists(filePath)) {
@@ -108,7 +110,7 @@ public class WmsFileManagementController {
@ResponseBody
public ResponseEntity<ApiResponseVo<String>> deleteFile(@PathVariable String fileName) {
try {
Path filePath = Paths.get(DIRECTORY_PATH, fileName);
Path filePath = Paths.get(directoryPath, fileName);
// 检查文件是否存在
if (!Files.exists(filePath)) {
@@ -149,7 +151,7 @@ public class WmsFileManagementController {
for (String fileName : fileNames) {
try {
Path filePath = Paths.get(DIRECTORY_PATH, fileName);
Path filePath = Paths.get(directoryPath, fileName);
if (Files.exists(filePath) && Files.isRegularFile(filePath)) {
Files.delete(filePath);
@@ -220,10 +222,10 @@ public class WmsFileManagementController {
*/
private ResponseEntity<ApiResponseVo<Map<String, Object>>> performCleanup() {
try {
Path directory = Paths.get(DIRECTORY_PATH);
Path directory = Paths.get(directoryPath);
if (!Files.exists(directory)) {
return ResponseEntity.ok(new ApiResponseVo<>(false, "目录不存在: " + DIRECTORY_PATH, null));
return ResponseEntity.ok(new ApiResponseVo<>(false, "目录不存在: " + directoryPath, null));
}
// 计算时间范围删除1-3个月前的文件
@@ -333,10 +335,10 @@ public class WmsFileManagementController {
@ResponseBody
public ResponseEntity<ApiResponseVo<Map<String, Object>>> getDirectoryStats() {
try {
Path directory = Paths.get(DIRECTORY_PATH);
Path directory = Paths.get(directoryPath);
if (!Files.exists(directory)) {
return ResponseEntity.ok(new ApiResponseVo<>(false, "目录不存在: " + DIRECTORY_PATH, null));
return ResponseEntity.ok(new ApiResponseVo<>(false, "目录不存在: " + directoryPath, null));
}
Map<String, Object> stats = new HashMap<>();
@@ -354,7 +356,7 @@ public class WmsFileManagementController {
}
}
stats.put("directoryPath", DIRECTORY_PATH);
stats.put("directoryPath", directoryPath);
stats.put("fileCount", fileCount);
stats.put("totalSize", totalSize);
stats.put("totalSizeMB", String.format("%.2f MB", totalSize / (1024.0 * 1024.0)));