本地文件bug修改
This commit is contained in:
@@ -2,6 +2,7 @@ package com.njcn.oss.utils;
|
|||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.lang.UUID;
|
import cn.hutool.core.lang.UUID;
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
import com.njcn.common.config.GeneralInfo;
|
import com.njcn.common.config.GeneralInfo;
|
||||||
import com.njcn.common.pojo.exception.BusinessException;
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
import com.njcn.huawei.obs.util.OBSUtil;
|
import com.njcn.huawei.obs.util.OBSUtil;
|
||||||
@@ -88,16 +89,23 @@ public class FileStorageUtil {
|
|||||||
}
|
}
|
||||||
}else {
|
}else {
|
||||||
try {
|
try {
|
||||||
// 确保目录存在
|
// 构建完整目录:基准目录 + dir子目录
|
||||||
Path uploadPath = Paths.get(generalInfo.getLocalStorePath());
|
Path basePath = Paths.get(generalInfo.getLocalStorePath());
|
||||||
|
Path uploadPath = basePath.resolve(dir); // 将dir作为子目录添加
|
||||||
|
|
||||||
|
// 确保完整目录存在(包括dir子目录)
|
||||||
if (!Files.exists(uploadPath)) {
|
if (!Files.exists(uploadPath)) {
|
||||||
Files.createDirectories(uploadPath);
|
Files.createDirectories(uploadPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成本地文件名(可以添加时间戳或UUID防止重名)
|
// 生成本地文件名(处理扩展名,防止异常)
|
||||||
String originalFilename = multipartFile.getOriginalFilename();
|
String originalFilename = multipartFile.getOriginalFilename();
|
||||||
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
|
String fileExtension = "";
|
||||||
String newFilename = UUID.randomUUID().toString() + fileExtension;
|
int extIndex = originalFilename.lastIndexOf(".");
|
||||||
|
if (extIndex > 0 && extIndex < originalFilename.length() - 1) {
|
||||||
|
fileExtension = originalFilename.substring(extIndex);
|
||||||
|
}
|
||||||
|
String newFilename = IdUtil.simpleUUID().toUpperCase() + fileExtension;
|
||||||
|
|
||||||
// 构建完整路径
|
// 构建完整路径
|
||||||
Path filePathPath = uploadPath.resolve(newFilename);
|
Path filePathPath = uploadPath.resolve(newFilename);
|
||||||
@@ -105,7 +113,7 @@ public class FileStorageUtil {
|
|||||||
// 保存文件
|
// 保存文件
|
||||||
multipartFile.transferTo(filePathPath.toFile());
|
multipartFile.transferTo(filePathPath.toFile());
|
||||||
|
|
||||||
// 返回相对路径或完整路径,根据需求调整
|
// 返回相对路径或完整路径
|
||||||
filePath = dir + File.separator + newFilename;
|
filePath = dir + File.separator + newFilename;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new BusinessException("本地文件上传失败: " + e.getMessage());
|
throw new BusinessException("本地文件上传失败: " + e.getMessage());
|
||||||
|
|||||||
Reference in New Issue
Block a user