如何在用户不上传任何图像的情况下编辑数据而不出错。

huangapple go评论57阅读模式
英文:

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);
    }
}

huangapple
  • 本文由 发表于 2023年2月16日 15:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定