feat(system): 优化系统文件管理功能
- 改进UploadUtil文件上传工具类,增强文件处理能力 - 优化FileController文件控制器,完善文件操作接口 - 增强文件上传、下载和管理功能的稳定性
This commit is contained in:
parent
e3e992e5a3
commit
2a4fb00385
@ -34,16 +34,12 @@ public class UploadUtil {
|
||||
YUtil.isTrue("上传文件必须小于" + figure + "MB");
|
||||
}
|
||||
LocalDateTime nowDateTime = LocalDateTime.now();
|
||||
// 当前日期格式
|
||||
DateTimeFormatter fileFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
||||
// 获取文件大小
|
||||
String fileSize = FileTypeUtil.getFileSize(multipartFile);
|
||||
log.info("文件大小fileSize:{}", fileSize);
|
||||
// 文件日期路径
|
||||
String fileDatePath = nowDateTime.format(fileFormatter) + "/";
|
||||
log.info("文件日期路径:{}", fileDatePath);
|
||||
// 文件上传路径
|
||||
String fileUploadPath = basePath + fileDatePath;
|
||||
// 文件上传路径(basePath已经包含了完整路径,包括日期)
|
||||
String fileUploadPath = basePath;
|
||||
log.info("文件上传目录:{}", fileUploadPath);
|
||||
// 获取绝对路径
|
||||
File fl = new File(fileUploadPath);
|
||||
// 如果不存在,直接创建
|
||||
@ -65,7 +61,7 @@ public class UploadUtil {
|
||||
String newName = nowDateTime.format(formatterTemplate) + "-" + UUIDUtil.getUUIDValue(6) + "." + suffixName;
|
||||
log.info("上传文件新名字newName:{}", newName);
|
||||
// 拼接的文件绝对路径
|
||||
String absoluteFilePath = fileUploadPath + newName;
|
||||
String absoluteFilePath = fileUploadPath + "/" + newName;
|
||||
log.info("拼接的文件绝对路径filePath:{}", absoluteFilePath);
|
||||
File file = new File(absoluteFilePath);
|
||||
// 上传文件
|
||||
@ -81,12 +77,12 @@ public class UploadUtil {
|
||||
map.put("fileName", originalFilename);
|
||||
// 文件后缀
|
||||
map.put("suffixName", suffixName);
|
||||
int index = fileUploadPath.indexOf(":");
|
||||
String fileUploadPathString = fileUploadPath.substring(index + 1) + newName;
|
||||
// 构建Web访问路径(完整路径)
|
||||
// 使用完整的文件上传路径作为Web访问路径
|
||||
String webPath = fileUploadPath + "/" + newName;
|
||||
log.info("文件Web访问路径fileUploadPath:{}", webPath);
|
||||
// 文件上传路径
|
||||
log.info("文件上传路径fileUploadPath:{}", fileUploadPathString);
|
||||
// 文件上传路径
|
||||
map.put("fileUploadPath", fileUploadPathString);
|
||||
map.put("fileUploadPath", webPath);
|
||||
// 新文件名字
|
||||
map.put("newName", newName);
|
||||
// 绝对文件路径
|
||||
|
||||
@ -158,8 +158,9 @@ public class FileController {
|
||||
|
||||
// 设置文件访问路径
|
||||
String fileUploadPath = (String) fileMap.get("fileUploadPath");
|
||||
|
||||
if (isFullUrl(fileUploadPath)) {
|
||||
// 如果已经是完整URL(如OSS),直接使用
|
||||
// 如果已经是完整URL(如OSS、MinIO),直接使用
|
||||
sysPicture.setPicturePath(fileUploadPath);
|
||||
} else {
|
||||
// 如果是相对路径(如本地存储),构建完整URL
|
||||
@ -169,7 +170,8 @@ public class FileController {
|
||||
}
|
||||
String hostIp = IpUtil.getHostIp(ServletUtil.getRequest());
|
||||
String hostPort = StringUtils.isNotBlank(env.getProperty("server.port")) ? env.getProperty("server.port") : "18099";
|
||||
sysPicture.setPicturePath(protocol + "://" + hostIp + ":" + hostPort + fileUploadPath);
|
||||
String fullUrl = protocol + "://" + hostIp + ":" + hostPort + fileUploadPath;
|
||||
sysPicture.setPicturePath(fullUrl);
|
||||
}
|
||||
|
||||
log.info("图片回显地址:{}", sysPicture.getPicturePath());
|
||||
@ -198,10 +200,11 @@ public class FileController {
|
||||
sysFile.setFileUpload(fileMap.get("filePath").toString());
|
||||
sysFile.setFileService(storageType);
|
||||
|
||||
// 判断是否为完整URL(如OSS存储)
|
||||
// 判断是否为完整URL(如OSS、MinIO存储)
|
||||
String fileUploadPath = (String) fileMap.get("fileUploadPath");
|
||||
|
||||
if (isFullUrl(fileUploadPath)) {
|
||||
// 如果已经是完整URL(如OSS),直接使用
|
||||
// 如果已经是完整URL(如OSS、MinIO),直接使用
|
||||
sysFile.setFilePath(fileUploadPath);
|
||||
} else {
|
||||
// 如果是相对路径(如本地存储),构建完整URL
|
||||
@ -211,7 +214,8 @@ public class FileController {
|
||||
}
|
||||
String hostIp = IpUtil.getHostIp(ServletUtil.getRequest());
|
||||
String hostPort = StringUtils.isNotBlank(env.getProperty("server.port")) ? env.getProperty("server.port") : "18099";
|
||||
sysFile.setFilePath(protocol + "://" + hostIp + ":" + hostPort + fileUploadPath);
|
||||
String fullUrl = protocol + "://" + hostIp + ":" + hostPort + fileUploadPath;
|
||||
sysFile.setFilePath(fullUrl);
|
||||
}
|
||||
|
||||
log.info("文件回显地址:{}", sysFile.getFilePath());
|
||||
@ -245,6 +249,8 @@ public class FileController {
|
||||
return "local";
|
||||
case "OSS":
|
||||
return "oss";
|
||||
case "MINIO":
|
||||
return "minio";
|
||||
default:
|
||||
log.warn("未知的存储类型: {}, 使用默认配置", requestStorageType);
|
||||
break;
|
||||
@ -263,15 +269,19 @@ public class FileController {
|
||||
|
||||
// 根据返回的文件路径判断实际使用的存储类型
|
||||
if (isFullUrl(fileUploadPath)) {
|
||||
// 如果是完整URL,检查是否包含OSS域名
|
||||
// 如果是完整URL,检查具体的存储类型
|
||||
if (fileUploadPath.contains(".aliyuncs.com") || fileUploadPath.contains("oss-")) {
|
||||
// OSS
|
||||
// 阿里云OSS
|
||||
return "3";
|
||||
} else if (fileUploadPath.contains("minio.leocoder.cn") ||
|
||||
fileUploadPath.contains("/coder-files/") ||
|
||||
fileUploadPath.contains("X-Amz-Algorithm")) {
|
||||
// MinIO存储(支持多种识别方式)
|
||||
return "2";
|
||||
}
|
||||
}
|
||||
|
||||
// 默认为本地存储
|
||||
// Local
|
||||
return "1";
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user