Heim  >  Fragen und Antworten  >  Hauptteil

Vermeiden Sie Fehler beim Bearbeiten von Daten, ohne Benutzerbilder hochzuladen

Als ich also die Daten für den Benutzer aktualisierte, bekam ich eine Fehlermeldung, dass das Bild erneut hochgeladen werden wollte, obwohl ich das Bild für den Benutzer bereits in der Datenbank hochgeladen hatte. Kann mir jemand sagen, wie ich den Benutzer speichern kann, ohne das Bild hochzuladen, aber das hochgeladene Bild in meiner Datenbank behalte?

Der Fehler ist java.IOException: Bilddatei konnte nicht gespeichert werden:

Verursacht durch: java.nio.file.DirectoryNotEmptyException: Profile-Image/4

Das ist mein 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";

P粉707235568P粉707235568185 Tage vor287

Antworte allen(1)Ich werde antworten

  • P粉156532706

    P粉1565327062024-04-01 09:38:55

    您的 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()+"File Path*******");
            Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException ioe) {
            throw new IOException("Could not save image file: " + fileName, ioe);
        }
    }

    Antwort
    0
  • StornierenAntwort