erge branch 'master' of https://github.com/RichZDS/BOSS
inti
This commit is contained in:
2026-01-10 22:17:02 +08:00
4 changed files with 128 additions and 64 deletions

View File

@@ -32,25 +32,28 @@ public class WebConfig implements WebMvcConfigurer {
*/
@PostConstruct
public void init() {
String projectRoot = System.getProperty("user.dir");
File configuredDir = new File(fileStoragePath);
if (!configuredDir.isAbsolute()) {
configuredDir = new File(projectRoot, fileStoragePath);
}
if (configuredDir.exists()) {
staticResourcePath = configuredDir.getAbsolutePath();
return;
}
try {
// 尝试获取classpath下的static目录
Resource resource = resourceLoader.getResource("classpath:/static/");
try {
File staticDir = resource.getFile();
staticResourcePath = staticDir.getAbsolutePath();
} catch (IOException e) {
// 如果在jar包中使用项目根目录
String projectRoot = System.getProperty("user.dir");
File staticDir = new File(projectRoot, "src/main/resources/static");
if (!staticDir.exists()) {
staticDir = new File(projectRoot, "target/classes/static");
}
staticResourcePath = staticDir.getAbsolutePath();
File staticDir = resource.getFile();
staticResourcePath = staticDir.getAbsolutePath();
} catch (IOException e) {
File targetStaticDir = new File(projectRoot, "target/classes/static");
if (targetStaticDir.exists()) {
staticResourcePath = targetStaticDir.getAbsolutePath();
} else {
staticResourcePath = new File(projectRoot, "src/main/resources/static").getAbsolutePath();
}
} catch (Exception e) {
// 降级方案:使用项目根目录
String projectRoot = System.getProperty("user.dir");
staticResourcePath = new File(projectRoot, "src/main/resources/static").getAbsolutePath();
}
}
@@ -78,4 +81,3 @@ public class WebConfig implements WebMvcConfigurer {
.setCachePeriod(3600);
}
}

View File

@@ -189,14 +189,16 @@ public class ResumeController {
String extension = "";
int lastDotIndex = originalFilename.lastIndexOf(".");
if (lastDotIndex > 0) {
extension = originalFilename.substring(lastDotIndex);
extension = originalFilename.substring(lastDotIndex).toLowerCase();
}
// 验证文件类型允许PDF和Word文档
String contentType = file.getContentType();
if (contentType != null && !contentType.equals("application/pdf")
&& !contentType.equals("application/msword")
&& !contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
boolean isAllowedContentType = "application/pdf".equals(contentType)
|| "application/msword".equals(contentType)
|| "application/vnd.openxmlformats-officedocument.wordprocessingml.document".equals(contentType);
boolean isAllowedExtension = ".pdf".equals(extension) || ".doc".equals(extension) || ".docx".equals(extension);
if (!isAllowedContentType && !isAllowedExtension) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "仅支持PDF和Word文档格式");
}

View File

@@ -27,6 +27,12 @@ public class FileService {
*/
private String basePath;
/**
* 文件存储路径(相对于项目根目录)
*/
@Value("${file.storage.path:src/main/resources/static}")
private String storagePath;
/**
* 文件访问基础URL用于构建访问地址
*/
@@ -44,36 +50,38 @@ public class FileService {
*/
@PostConstruct
public void init() {
try {
// 获取src/main/resources/static目录的绝对路径
Resource resource = resourceLoader.getResource("classpath:/static/");
File staticDir;
try {
// 尝试获取资源文件的实际路径
staticDir = resource.getFile();
} catch (IOException e) {
// 如果在jar包中运行无法直接获取File则使用项目根目录下的路径
String projectRoot = System.getProperty("user.dir");
staticDir = new File(projectRoot, "src/main/resources/static");
// 如果项目根目录下不存在尝试使用target/classes/static
if (!staticDir.exists()) {
staticDir = new File(projectRoot, "target/classes/static");
}
}
this.basePath = staticDir.getAbsolutePath();
// 确保目录存在
initStorageDirectory();
} catch (Exception e) {
log.error("初始化文件存储目录失败", e);
// 降级方案使用项目根目录下的static目录
String projectRoot = System.getProperty("user.dir");
this.basePath = new File(projectRoot, "src/main/resources/static").getAbsolutePath();
initStorageDirectory();
this.basePath = resolveBasePath();
initStorageDirectory();
log.info("文件存储路径已初始化: {}", basePath);
}
private String resolveBasePath() {
String projectRoot = System.getProperty("user.dir");
Path configuredPath = Paths.get(storagePath);
Path resolvedPath = configuredPath.isAbsolute()
? configuredPath
: Paths.get(projectRoot).resolve(configuredPath);
if (Files.exists(resolvedPath)) {
return resolvedPath.toAbsolutePath().toString();
}
try {
Resource resource = resourceLoader.getResource("classpath:/static/");
File staticDir = resource.getFile();
if (staticDir.exists()) {
return staticDir.getAbsolutePath();
}
} catch (IOException e) {
log.warn("无法从classpath解析静态目录尝试降级路径: {}", e.getMessage());
}
Path targetPath = Paths.get(projectRoot, "target/classes/static");
if (Files.exists(targetPath)) {
return targetPath.toAbsolutePath().toString();
}
return resolvedPath.toAbsolutePath().toString();
}
/**
@@ -216,4 +224,3 @@ public class FileService {
return filePath.toAbsolutePath().toString();
}
}