简体   繁体   中英

Spring Boot file upload issue - Could not store the file error only occur after few days

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

It will work back after restarted the RESTFul JAR application.

Error

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM