java.nio.file.FileSystemNotFoundException when getting file from resources folder

前端 未结 5 1725
野的像风
野的像风 2021-01-04 11:58

I am getting this error on the following code (note that this does not happen on my local machine, only on my build server):

Files.readAllBytes(Paths.get(get         


        
相关标签:
5条回答
  • 2021-01-04 12:09

    Generally, it is not correct to assume that every resource is a file. Instead, you should obtain the URL/InputStream for that resource and read the bytes from there. Guava can help:

    URL url = getClass().getResource("/elasticsearch/segmentsIndex.json");
    String content = Resources.toString(url, charset);
    


    Another possible solution, with the InputStream and apache commons: Convert InputStream to byte array in Java .

    From a byte[], simply use the String constructor to obtain the content as a string.

    0 讨论(0)
  • 2021-01-04 12:15

    If you use Spring, inject resources. Be it a file, or folder, or even multiple files, there are chances, you can do it via injection. Warning: DO NOT use File and Files.walk with the injected resources, otherwise you'll get FileSystemNotFoundException when running as JAR.

    This example demonstrates the injection of multiple images located in static/img folder.

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.io.Resource;
    import org.springframework.stereotype.Service;
    import javax.annotation.PostConstruct;
    
    @Service
    public class StackoverflowService {
    
        @Value("classpath:static/img/*")
        private Resource[] resources;
        private List<String> filenames;
    
        @PostConstruct
        void init() {
            final Predicate<String> isJPG = path -> path.endsWith(".jpg");
            final Predicate<String> isPNG = path -> path.endsWith(".png");
    
            // iterate resources, filter by type and get filenames
            filenames = Arrays.stream(resources)
                    .map(Resource::getFilename)
                    .filter(isJPG.or(isPNG))
                    .collect(Collectors.toList());
        }
    }
    
    0 讨论(0)
  • 2021-01-04 12:17

    You should get the resource through an InputStream and not a File, but there is no need for external libraries.

    All you need is a couple of lines of code:

    InputStream is = getClass().getResourceAsStream("/elasticsearch/segmentsIndex.json");
    java.util.Scanner scanner = new java.util.Scanner(is).useDelimiter("\\A");
    String json = scanner.hasNext() ? scanner.next() : "";
    

    You can learn more about that method at https://stackoverflow.com/a/5445161/968244

    0 讨论(0)
  • 2021-01-04 12:22

    You should be using getResourceAsStream(…) instead of getResource(…). There are a number of methods to read all bytes into a byte array, e.g. Apache Commons has a utility method to do just that.

    0 讨论(0)
  • 2021-01-04 12:26

    Don't try to access a resource like a file. Just grab the InputStream and read the data from there:

    byte[] data;
    try (InputStream in = getClass().getResourceAsStream("/elasticsearch/segmentsIndex.json")) {
        data = in.readAllBytes​(); // usable in Java 9+
        // data = IOUtils.toByteArray(in); // uses Apache commons IO library
    }
    

    This example uses the IOUtils class from Apache commons-io library.

    If you are targeting Java 9+ you can alternatively use data = in.readAllBytes​();.

    0 讨论(0)
提交回复
热议问题