refactor(music-houduan): 优化头像上传逻辑- 在 SingerServiceImpl 和 UserController 中添加 File 类的导入
- 修改头像上传逻辑,将物理路径转换为相对于项目目录的路径 - 统一处理头像上传的方式,提高代码复用性
This commit is contained in:
@@ -15,6 +15,8 @@ import org.springframework.validation.annotation.Validated;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户控制器
|
* 用户控制器
|
||||||
*/
|
*/
|
||||||
@@ -216,7 +218,8 @@ public class UserController {
|
|||||||
@PostMapping("/upload/avatar")
|
@PostMapping("/upload/avatar")
|
||||||
public Result<String> uploadAvatar(@Parameter(description = "头像文件") @RequestParam("file") MultipartFile file) {
|
public Result<String> uploadAvatar(@Parameter(description = "头像文件") @RequestParam("file") MultipartFile file) {
|
||||||
// 上传头像
|
// 上传头像
|
||||||
String avatarUrl = fileService.uploadImage(file);
|
String avatarPhysicalPath = fileService.uploadImage(file);
|
||||||
|
String avatarUrl = avatarPhysicalPath.replace(System.getProperty("user.dir") + File.separator + "music-houduan", "").replace(File.separator, "/");
|
||||||
|
|
||||||
// 用户ID已经通过 SaCheckLogin 注解验证
|
// 用户ID已经通过 SaCheckLogin 注解验证
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import org.springframework.util.StringUtils;
|
|||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import jakarta.persistence.criteria.Predicate;
|
import jakarta.persistence.criteria.Predicate;
|
||||||
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -206,7 +207,8 @@ public class SingerServiceImpl implements SingerService {
|
|||||||
// 上传头像
|
// 上传头像
|
||||||
String avatarUrl = null;
|
String avatarUrl = null;
|
||||||
if (avatar != null && !avatar.isEmpty()) {
|
if (avatar != null && !avatar.isEmpty()) {
|
||||||
avatarUrl = fileService.uploadImage(avatar);
|
String avatarPhysicalPath = fileService.uploadImage(avatar);
|
||||||
|
avatarUrl = avatarPhysicalPath.replace(System.getProperty("user.dir") + File.separator + "music-houduan", "").replace(File.separator, "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建歌手实体
|
// 创建歌手实体
|
||||||
@@ -250,7 +252,8 @@ public class SingerServiceImpl implements SingerService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 上传新头像
|
// 上传新头像
|
||||||
String avatarUrl = fileService.uploadImage(avatar);
|
String avatarPhysicalPath = fileService.uploadImage(avatar);
|
||||||
|
String avatarUrl = avatarPhysicalPath.replace(System.getProperty("user.dir") + File.separator + "music-houduan", "").replace(File.separator, "/");
|
||||||
singer.setAvatar(avatarUrl);
|
singer.setAvatar(avatarUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user