用户头像
Change-Id: I562c0cb7212c1ac9c7ad1dad96b136fc8c23269c
diff --git a/src/main/java/com/example/myproject/controller/UserController.java b/src/main/java/com/example/myproject/controller/UserController.java
index 224c138..6b3d793 100644
--- a/src/main/java/com/example/myproject/controller/UserController.java
+++ b/src/main/java/com/example/myproject/controller/UserController.java
@@ -6,8 +6,11 @@
import com.example.myproject.service.TaskService;
import com.example.myproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Optional;
@@ -103,14 +106,13 @@
@RequestBody Map<String, Object> profileData) {
// 获取请求体中的修改数据
- String avatarUrl = (String) profileData.get("avatarUrl");
String nickname = (String) profileData.get("nickname");
String gender = (String) profileData.get("gender");
String description = (String) profileData.get("description");
String hobbies = (String) profileData.get("hobbies");
// 调用服务层方法进行修改
- boolean updated = userService.editProfile(userId, avatarUrl, nickname, gender, description, hobbies);
+ boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies);
// 返回操作结果消息
if (updated) {
@@ -149,6 +151,14 @@
return friends;
}
+ @PostMapping("/{userId}/uploadAvatar")
+ public Map<String, Object> uploadAvatar(
+ @PathVariable Long userId,
+ @RequestParam("file") MultipartFile file) {
+ return userService.uploadUserAvatar(userId, file);
+ }
+
+
}
diff --git a/src/main/java/com/example/myproject/repository/UserRepository.java b/src/main/java/com/example/myproject/repository/UserRepository.java
index 6927c5f..c61a79a 100644
--- a/src/main/java/com/example/myproject/repository/UserRepository.java
+++ b/src/main/java/com/example/myproject/repository/UserRepository.java
@@ -6,5 +6,6 @@
public interface UserRepository extends JpaRepository<Users, Long> {
Optional<Users> findByEmail(String email);
+
Optional<Users> findByUsername(String username);
}
diff --git a/src/main/java/com/example/myproject/service/UserService.java b/src/main/java/com/example/myproject/service/UserService.java
index 0d37854..283a85c 100644
--- a/src/main/java/com/example/myproject/service/UserService.java
+++ b/src/main/java/com/example/myproject/service/UserService.java
@@ -1,12 +1,21 @@
package com.example.myproject.service;
+import com.example.myproject.entity.User;
import com.example.myproject.entity.Users;
import com.example.myproject.entity.UserInviteCode;
import com.example.myproject.repository.UserRepository;
import com.example.myproject.repository.UserInviteCodeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+import javax.servlet.http.HttpServletRequest;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.time.LocalDateTime;
import java.util.*;
@Service
@@ -207,7 +216,7 @@
}
// 修改用户个人资料
- public boolean editProfile(Long userId, String avatarUrl, String nickname, String gender, String description, String hobbies) {
+ public boolean editProfile(Long userId, String nickname, String gender, String description, String hobbies) {
Optional<Users> userOptional = userRepository.findById(userId);
// 如果用户不存在,返回false
@@ -218,9 +227,6 @@
Users user = userOptional.get();
// 更新用户资料,只有传入值才会更新对应字段
- if (avatarUrl != null) {
- user.setAvatarUrl(avatarUrl);
- }
if (nickname != null) {
user.setUsername(nickname);
}
@@ -266,4 +272,40 @@
}
+ private static final String AVATAR_DIR = "uploads/avatarUrl/";
+
+ public Map<String, Object> uploadUserAvatar(Long userId, MultipartFile file) {
+ Users user = userRepository.findById(userId)
+ .orElseThrow(() -> new RuntimeException("用户不存在"));
+
+ try {
+ String avatarUrl = saveAvatar(file, userId);
+ user.setAvatarUrl(avatarUrl);
+ userRepository.save(user);
+
+ Map<String, Object> response = new HashMap<>();
+ response.put("status", "success");
+ response.put("message", "头像上传成功");
+ response.put("userId", user.getUserId());
+ response.put("avatarUrl", avatarUrl);
+ return response;
+
+ } catch (IOException e) {
+ throw new RuntimeException("头像上传失败: " + e.getMessage());
+ }
+ }
+
+ // 保存头像文件并返回可访问 URL
+ public String saveAvatar(MultipartFile file, Long userId) throws IOException {
+ String originalFilename = file.getOriginalFilename();
+ String extension = originalFilename.substring(originalFilename.lastIndexOf('.'));
+ String fileName = userId + extension; // 以用户ID作为文件名
+ Path path = Paths.get(AVATAR_DIR + fileName);
+ Files.createDirectories(path.getParent());
+ Files.write(path, file.getBytes());
+ return "/" + AVATAR_DIR + fileName; // 返回相对URL路径
+ }
+
+
+
}