优化 本地文件保存 target的问题、修改为存储将保存到项目上级目录下,用于持久化

This commit is contained in:
YuanFeng 2024-07-21 09:56:06 +08:00
parent 18f4f64ae5
commit 6e2e264e07
1 changed files with 23 additions and 1 deletions

View File

@ -44,9 +44,31 @@ public class LocalFileClient extends AbstractFileClient<LocalFileClientConfig> {
String filePath = getFilePath(path);
return FileUtil.readBytes(filePath);
}
private static final String SAVE_DIRECTORY;
static {
// 将文件保存到项目上级目录下 uploads
String dir = System.getProperty("user.dir") + File.separator;
File file = new File(dir);
String parent = file.getParent();
// 检查是否有父目录
if (parent != null) {
file = new File(parent, "uploads");
} else {
file = new File(dir, "uploads");
}
if (!file.exists()) {
file.mkdirs();
}
SAVE_DIRECTORY = file.getPath();
}
public static String getSaveDirectory() {
return SAVE_DIRECTORY;
}
private String getFilePath(String path) {
return config.getBasePath() + path;
// 设置为绝对路径
return getSaveDirectory()+ File.separator+config.getBasePath() + path;
}
}