So when I was updating the data for the user, I ran into an error where it wanted to upload the image again even though I had already uploaded the image for the user in the database. Can anyone tell me how to save the user without uploading the image, but keep the uploaded image in my database?
The error is java.IOException: Unable to save image file:
Caused by: java.nio.file.DirectoryNotEmptyException: profile-image/4
This is my controller
@PostMapping("/profile/save") public String saveProfile(Profile profile, Model model, @RequestParam("image") MultipartFile multipartFile,@RequestParam(value="userId", required=false) Long userId) throws IOException { profile.setUser(userRepo.findById(userId).get()); String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename()) ; String uploadDir - "profile-image/"+ profile.getId(); FileUploadUtil.saveFile(uploadDir, fileName, multipartFile); profile.setProfileImg(fileName); profileService.saveProfile(profile); return "redirect:/user/profile";
There is a bug in your
FileUploadUtil
class. This directory should be created only if the directory does not exist exists: