Search code examples
javaspringdockerfilespring-rest

Java throws java.nio.file.NoSuchFileException, but file exists


I'm running a Spring REST application inside a docker container. I have a function inside a Spring controller for saving images and a function for reading them. The function for saving works properly but I have an issue with the function for reading them:

public byte[] getByteArray(String fileName) {
    try {
        File f  = new File("/upload/" + fileName);
        return Files.readAllBytes(f.toPath());
    } catch (IOException e) {
        e.printStackTrace(); // this is for testing
        return null;
    }
}

However after I use the above function I get this error java.nio.file.NoSuchFileException: /upload/test.png. I checked and this file exists in this directory. What could be the reason Java can't see this file?


Solution

  • Most likely your /upload directory is not accessible to the java process. directories have access rights, an owner, and a group. There is one set of rights for the owner, one for the group, and one for the rest.