feat: 个人信息修改
This commit is contained in:
@@ -31,6 +31,15 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
/** 头像存储目录(相对于项目工作目录) */
|
||||
private static final String AVATAR_DIR = "uploads/avatars";
|
||||
|
||||
/**
|
||||
* 获取头像存储的绝对路径目录
|
||||
*
|
||||
* 使用 user.dir 构建绝对路径,避免 Tomcat 临时工作目录导致 FileNotFoundException。
|
||||
*/
|
||||
private Path getAvatarDir() {
|
||||
return Paths.get(System.getProperty("user.dir"), AVATAR_DIR).toAbsolutePath();
|
||||
}
|
||||
/** 允许的头像文件类型 */
|
||||
private static final Set<String> ALLOWED_CONTENT_TYPES = Set.of(
|
||||
"image/jpeg", "image/png", "image/webp", "image/gif"
|
||||
@@ -109,8 +118,8 @@ public class UserServiceImpl implements UserService {
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
|
||||
|
||||
try {
|
||||
// 确保目录存在
|
||||
Path avatarDir = Paths.get(AVATAR_DIR);
|
||||
// 确保目录存在(使用绝对路径,避免 Tomcat 临时目录问题)
|
||||
Path avatarDir = getAvatarDir();
|
||||
Files.createDirectories(avatarDir);
|
||||
|
||||
// 删除旧头像文件(如果存在且不是默认 SVG)
|
||||
@@ -163,7 +172,9 @@ public class UserServiceImpl implements UserService {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Path oldFile = Paths.get(avatarPath.substring(1)); // 去掉开头的 /
|
||||
// 从 URL 路径中提取文件名,使用绝对路径删除
|
||||
String filename = avatarPath.substring(avatarPath.lastIndexOf('/') + 1);
|
||||
Path oldFile = getAvatarDir().resolve(filename);
|
||||
Files.deleteIfExists(oldFile);
|
||||
log.debug("[用户服务] 旧头像已删除: {}", oldFile);
|
||||
} catch (IOException e) {
|
||||
|
||||
Reference in New Issue
Block a user