构建Rest风格参考上一篇博客
1.Controller
@PostMapping("/photo")
public String uploadphoto(@RequestBody() @PathVariable("file") MultipartFile file,
User user) throws IOException {
if (file.isEmpty()) {
return "{msg:文件不存在!请重新上传}";
}
String tempFileName = UUID.randomUUID().toString().replace("-", "");
String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
File file1 = new File("D:/photo/" + tempFileName + "." + suffix);
file.transferTo(file1);
user.setPhoto(file1.getAbsolutePath());
uSerService.updateUser(user);
return "{msg:成功!}";
}
2.使用PostMan测试文件上传功能
参考: