1

I have a RESTful API created using Java Spring Boot 2.4.2.

1 of the main issue that I encountered recently is that, the Multipart file upload is working fine but the same code will not work after couple of days. It will work back after restarted the RESTFul JAR application.

The error that been display in Postman: Could not store the file. Error

The relevant code to this is here:

try {
        FileUploadUtil.saveFile(uploadPath, file.getOriginalFilename(), file);
    } catch (IOException e) {
        throw new RuntimeException("Could not store the file. Error: " + e.getMessage());
    }

And the FileUploadUtil class:

public class FileUploadUtil {

public static void saveFile(String uploadDir, 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);
        Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException ioe) {
        throw new IOException("Could not save uploaded file: " + fileName, ioe);
    }
}

public static File fileFor(String uploadDir, String id) {
    return new File(uploadDir, id);
}}

And the main POST API method head that called the first part of the code above is:

    @PostMapping(value = "/clients/details/{clientDetailsId}/files/{department}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PreAuthorize("hasAuthority('PERSONNEL') or hasAuthority('CUSTODIAN') or hasAuthority('ADMIN')")
public ResponseEntity<ClientDetails> createClientDetailsFiles(@PathVariable("clientDetailsId") long clientDetailsId,
        @PathVariable("department") String department,
        @RequestPart(value = "FORM_SEC_58", required = false) MultipartFile[] FORM_SEC_58_file,@RequestPart(value = "FORM_SEC_78", required = false) MultipartFile[] FORM_SEC_78_file,
        @RequestPart(value = "FORM_SEC_105", required = false) MultipartFile[] FORM_SEC_105_file,
        @RequestPart(value = "FORM_SEC_51", required = false) MultipartFile[] FORM_SEC_51_file,
        @RequestPart(value = "FORM_SEC_76", required = false) MultipartFile[] FORM_SEC_76_file)

And the application.properties side:

spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=90MB
spring.servlet.multipart.max-request-size=90MB

Can anyone advise what is the issue ya?

Ken
  • 37
  • 1
  • 8

1 Answers1

0

I had the same issue, it was working file once, but after some time, it stopped working. I am almost certain your issue is this, because, we have the same code and our scenario is the same. The way I figured it out was:

Debugging

I added a statement to print the error to see what was actually going on, what you are currently doing is only taking message of error, not the whole error. So change it to:

try {
    FileUploadUtil.saveFile(uploadPath, file.getOriginalFilename(), file);
} catch (IOException e) {
    e.printStackTrace();
    throw new RuntimeException("Could not store the file. Error: " + e.getMessage());
}

Actual Problem

And the error was FileAlreadyExistsException FileAlreadyExistsException

Basically what that means is that you are trying to upload a file with same name twice.

Solution

To fix this issue you can use different approaches. One of them is to generate UUID for file and store it also in database, to access later.

27px
  • 430
  • 5
  • 16