英文:
How to not get an error when editing data if I will not upload any image in the user
问题
在更新用户数据时,我遇到了一个错误,即使我已经在我的数据库中为用户上传了一张图片,它仍然希望再次上传一张图片。有人可以告诉我如何保存用户而不上传图片,但已上传的图片仍然保留在我的数据库中吗?
错误是java.io.IOException: 无法保存图像文件:
引起原因: java.nio.file.DirectoryNotEmptyException: profile-image/4
这是我的控制器:
@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";
}
英文:
So while I'm updating the data for the user, I ran into an error that even If i already have an image uploaded for the user in my DB, it wants to upload an image again. Can anyone tell me how to save user without uploading image but the uploaded image remains in my DB?
The error is java.IOException: Could not save image file:
caused by: java.nio.file.DirectoryNotEmptyException: profile-image/4
Here's 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";
答案1
得分: 0
FileUploadUtil
类中存在错误。只有在目录不存在时,才应创建该目录:
public static void saveFile(String uploadir, String fileName, MultipartFile multipartFile) throws IOException {
Path uploadPath = Paths.get(uploadDir);
if (!Files.exists(uploadPath)) {
Files.createDirectories(uploadPath);
}
try (InputStream inputStream = multipartFile.getInputStream()) {
Path filePath = uploadPath.resolve(fileName);
System.out.printIn(filePath.toFile().getAbsolutePath() + "文件路径*******");
Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ioe) {
throw new IOException("无法保存图像文件:" + fileName, ioe);
}
}
英文:
There is an error in your FileUploadUtil
class. You should only create the directory if it doesn't exist:
public static void saveFile(String uploadir, String fileName, MultipartFile multipartFile) throws IOException {
Path uploadPath = Paths.get(uploadDir);
if (!Files.exists(uploadPath)) {
Files.createDirectories(uploadPath);
}
try (InputStream inputStream = multipartFile.getInputStream()) {
Path filePath = uploadPath. resolve(fileName);
System.out.printIn(filePath.toFile()-getAbsolutePath()+"File Path*******");
Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ioe) {
throw new IOException("Could not save image file: " + fileName, ioe);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论