diff --git a/src/main/java/com/bigdata/wxappserver/controller/UserController.java b/src/main/java/com/bigdata/wxappserver/controller/UserController.java index d732ab1..7209cbf 100644 --- a/src/main/java/com/bigdata/wxappserver/controller/UserController.java +++ b/src/main/java/com/bigdata/wxappserver/controller/UserController.java @@ -4,10 +4,15 @@ import com.alibaba.fastjson.JSONObject; import com.bigdata.wxappserver.service.UserService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; /** * Created with IntelliJ IDEA. @@ -22,7 +27,6 @@ import org.springframework.web.bind.annotation.RestController; @Slf4j public class UserController { - @Autowired UserService userService; @@ -43,6 +47,10 @@ public class UserController { @RequestMapping("delete") public JSONObject delete(@RequestParam("id")Integer id){ return userService.delete(id); + } + @PostMapping("/upload") + public JSONObject upload(@RequestParam("file") MultipartFile file) { + return userService.upload(file); } diff --git a/src/main/java/com/bigdata/wxappserver/entity/User.java b/src/main/java/com/bigdata/wxappserver/entity/User.java index 1cc06a7..42d0737 100644 --- a/src/main/java/com/bigdata/wxappserver/entity/User.java +++ b/src/main/java/com/bigdata/wxappserver/entity/User.java @@ -26,5 +26,9 @@ public class User extends Base{ * 用户地址 */ private String address; + /** + * 用户头像 + */ + private String avatarUrl; } diff --git a/src/main/java/com/bigdata/wxappserver/service/UserService.java b/src/main/java/com/bigdata/wxappserver/service/UserService.java index 4d83bd8..32e8073 100644 --- a/src/main/java/com/bigdata/wxappserver/service/UserService.java +++ b/src/main/java/com/bigdata/wxappserver/service/UserService.java @@ -11,13 +11,22 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +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.util.Collections; import java.util.List; import java.util.stream.Collectors; @Service public class UserService extends ServiceImpl { + private static final String UPLOAD_DIR = "uploads/"; + @Transactional(rollbackFor = Exception.class) public JSONObject addOrUpdate(JSONObject jsonObject) { User user = jsonObject.toJavaObject(User.class); @@ -77,4 +86,30 @@ public class UserService extends ServiceImpl { return new JSONObject().fluentPut("data", user); } + public JSONObject upload(MultipartFile file) { + // 检查上传文件是否为空 + if (file.isEmpty()) { + return new JSONObject().fluentPut("message", "上传文件为空").fluentPut("success", false); + } + try { + // 创建上传目录 + File directory = new File(UPLOAD_DIR); + if (!directory.exists()) { + directory.mkdirs(); + } + // 保存文件到服务器 + String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename(); + Path path = Paths.get(UPLOAD_DIR + fileName); + Files.write(path, file.getBytes()); + // 构建文件的访问URL + String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath() + .path("/" + UPLOAD_DIR) + .path(fileName) + .toUriString(); + return new JSONObject().fluentPut("success", true).fluentPut("fileDownloadUri", fileDownloadUri); + } catch (IOException e) { + e.printStackTrace(); + return new JSONObject().fluentPut("success", false).fluentPut("message", "上传文件失败"); + } + } } diff --git a/uploads/1725289271512_6Ebn1Z6ZFJwb7c29657b26486bef5fe964055080d924.jpeg b/uploads/1725289271512_6Ebn1Z6ZFJwb7c29657b26486bef5fe964055080d924.jpeg new file mode 100644 index 0000000..6aa4c8b Binary files /dev/null and b/uploads/1725289271512_6Ebn1Z6ZFJwb7c29657b26486bef5fe964055080d924.jpeg differ diff --git a/uploads/1725289397677_AsuddxS2f8Wka28e1fa76ed0d8ee4c39351bbd531128.jpeg b/uploads/1725289397677_AsuddxS2f8Wka28e1fa76ed0d8ee4c39351bbd531128.jpeg new file mode 100644 index 0000000..a914581 Binary files /dev/null and b/uploads/1725289397677_AsuddxS2f8Wka28e1fa76ed0d8ee4c39351bbd531128.jpeg differ diff --git a/uploads/1725289488986_5Vb62KvGnI7u92aa7dbf48b618e77506ab4fe132cacb.jpeg b/uploads/1725289488986_5Vb62KvGnI7u92aa7dbf48b618e77506ab4fe132cacb.jpeg new file mode 100644 index 0000000..800097c Binary files /dev/null and b/uploads/1725289488986_5Vb62KvGnI7u92aa7dbf48b618e77506ab4fe132cacb.jpeg differ diff --git a/uploads/1725289650515_JM0iwEO9zyZjb63ba3717733d9807843383277a6b2ee.jpeg b/uploads/1725289650515_JM0iwEO9zyZjb63ba3717733d9807843383277a6b2ee.jpeg new file mode 100644 index 0000000..e1c52d3 Binary files /dev/null and b/uploads/1725289650515_JM0iwEO9zyZjb63ba3717733d9807843383277a6b2ee.jpeg differ diff --git a/uploads/1725289919366_RjMlkbew6OTp8b5515d01d52c4ad9a558375ce6cf5a5.jpeg b/uploads/1725289919366_RjMlkbew6OTp8b5515d01d52c4ad9a558375ce6cf5a5.jpeg new file mode 100644 index 0000000..024eb31 Binary files /dev/null and b/uploads/1725289919366_RjMlkbew6OTp8b5515d01d52c4ad9a558375ce6cf5a5.jpeg differ