@Test public void upload() throws Exception { File logoFile = new File("C:\\Users\\zh564\\Pictures\\logo.png"); MockMultipartFile file = new MockMultipartFile("file", "logo.png", MediaType.IMAGE_PNG_VALUE, new FileInputStream(logoFile)); mockMvc.perform(multipart("/user/imgUpload").file(file)).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code))); mockMvc.perform(multipart("/user/imgUpload").file(file).header("Authorization", userLogin())).andDo(result -> { JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); assertEquals(SUCCESS.getCode(), object.getInt(Code)); assertNotNull(object.getString(Result)); }); }
@PostMapping("/user/imgUpload") @ResponseBody public Response upload(@RequestParam("file") MultipartFile file) throws IOException { if (file.isEmpty()) { return ResponseUtil.failure("上传失败，请选择文件"); } String fileName = file.getOriginalFilename(); String mime = fileName.substring(fileName.lastIndexOf(".")); if (".png".equals(mime.toLowerCase()) || ".jpg".equals(mime.toLowerCase()) || ".jpeg".equals(mime.toLowerCase()) || ".bmp".equals(mime.toLowerCase())) { return (Response) userService.updateUserAavatarImg(file.getInputStream(), mime); } return ResponseUtil.failure("请上传图片文件"); }
UserController { @PostMapping("/user/imgUpload") @ResponseBody public Response upload(@RequestParam("file") MultipartFile file) throws IOException { if (file.isEmpty()) { return ResponseUtil.failure("上传失败，请选择文件"); } String fileName = file.getOriginalFilename(); String mime = fileName.substring(fileName.lastIndexOf(".")); if (".png".equals(mime.toLowerCase()) || ".jpg".equals(mime.toLowerCase()) || ".jpeg".equals(mime.toLowerCase()) || ".bmp".equals(mime.toLowerCase())) { return (Response) userService.updateUserAavatarImg(file.getInputStream(), mime); } return ResponseUtil.failure("请上传图片文件"); } }
UserController { @PostMapping("/user/imgUpload") @ResponseBody public Response upload(@RequestParam("file") MultipartFile file) throws IOException { if (file.isEmpty()) { return ResponseUtil.failure("上传失败，请选择文件"); } String fileName = file.getOriginalFilename(); String mime = fileName.substring(fileName.lastIndexOf(".")); if (".png".equals(mime.toLowerCase()) || ".jpg".equals(mime.toLowerCase()) || ".jpeg".equals(mime.toLowerCase()) || ".bmp".equals(mime.toLowerCase())) { return (Response) userService.updateUserAavatarImg(file.getInputStream(), mime); } return ResponseUtil.failure("请上传图片文件"); }  }
UserController { @PostMapping("/user/imgUpload") @ResponseBody public Response upload(@RequestParam("file") MultipartFile file) throws IOException { if (file.isEmpty()) { return ResponseUtil.failure("上传失败，请选择文件"); } String fileName = file.getOriginalFilename(); String mime = fileName.substring(fileName.lastIndexOf(".")); if (".png".equals(mime.toLowerCase()) || ".jpg".equals(mime.toLowerCase()) || ".jpeg".equals(mime.toLowerCase()) || ".bmp".equals(mime.toLowerCase())) { return (Response) userService.updateUserAavatarImg(file.getInputStream(), mime); } return ResponseUtil.failure("请上传图片文件"); }  @PostMapping("/login") Response login(@RequestBody LoginReq loginReq); @PostMapping("/registration") Response registration(@RequestParam("email") String email,
                                 @RequestParam("password") String password); @GetMapping("/logout") Response logout(); @PutMapping("/user/userInfo/update") Response updateInfo(String desc, String displayName); @GetMapping("/user/userInfo") Response getUserInfo(); @PostMapping("/user/imgUpload") @ResponseBody Response upload(@RequestParam("file") MultipartFile file); @PostMapping("/sendResetPwdEmail") Response sendResetPwdEmail(@RequestParam("email") String email); @PostMapping("/sendVerifyEmail") Response sendVerifyEmail(@RequestParam("email") String email); @PostMapping("/emailVerify") Response emailVerify(@RequestParam("verifyId") String verifyId,
                                @RequestParam("email") String mail); @PostMapping("/resetPwd") Response resetPwd(@RequestParam("verifyId") String verifyId,
                             @RequestParam("email") String email,
                             @RequestParam("pwd") String pwd); @DeleteMapping("/admin/user/delete") Response multipleDelete(@RequestBody Integer[] ids); @DeleteMapping("/admin/user/delete/{id}") Response delete(@PathVariable("id") Integer id); @PutMapping("/admin/user") Response updateInfoByAdmin(@RequestBody UserReq user); @GetMapping("/admin/users") Response getAllUser(@RequestParam("page") int pageNum, @RequestParam("count") int count); @GetMapping("/emailStatus/{email}") Response getEmailStatus(@PathVariable("email") String email); }
UserController { @PostMapping("/user/imgUpload") @ResponseBody public Response upload(@RequestParam("file") MultipartFile file) throws IOException { if (file.isEmpty()) { return ResponseUtil.failure("上传失败，请选择文件"); } String fileName = file.getOriginalFilename(); String mime = fileName.substring(fileName.lastIndexOf(".")); if (".png".equals(mime.toLowerCase()) || ".jpg".equals(mime.toLowerCase()) || ".jpeg".equals(mime.toLowerCase()) || ".bmp".equals(mime.toLowerCase())) { return (Response) userService.updateUserAavatarImg(file.getInputStream(), mime); } return ResponseUtil.failure("请上传图片文件"); }  @PostMapping("/login") Response login(@RequestBody LoginReq loginReq); @PostMapping("/registration") Response registration(@RequestParam("email") String email,
                                 @RequestParam("password") String password); @GetMapping("/logout") Response logout(); @PutMapping("/user/userInfo/update") Response updateInfo(String desc, String displayName); @GetMapping("/user/userInfo") Response getUserInfo(); @PostMapping("/user/imgUpload") @ResponseBody Response upload(@RequestParam("file") MultipartFile file); @PostMapping("/sendResetPwdEmail") Response sendResetPwdEmail(@RequestParam("email") String email); @PostMapping("/sendVerifyEmail") Response sendVerifyEmail(@RequestParam("email") String email); @PostMapping("/emailVerify") Response emailVerify(@RequestParam("verifyId") String verifyId,
                                @RequestParam("email") String mail); @PostMapping("/resetPwd") Response resetPwd(@RequestParam("verifyId") String verifyId,
                             @RequestParam("email") String email,
                             @RequestParam("pwd") String pwd); @DeleteMapping("/admin/user/delete") Response multipleDelete(@RequestBody Integer[] ids); @DeleteMapping("/admin/user/delete/{id}") Response delete(@PathVariable("id") Integer id); @PutMapping("/admin/user") Response updateInfoByAdmin(@RequestBody UserReq user); @GetMapping("/admin/users") Response getAllUser(@RequestParam("page") int pageNum, @RequestParam("count") int count); @GetMapping("/emailStatus/{email}") Response getEmailStatus(@PathVariable("email") String email);  }