Kyle Bridenstine
Kyle Bridenstine

Reputation: 6373

Locate static files under /resource/ directory in Spring Boot

I have a spring boot application with embedded Tomcat that I'm testing through Netbeans. Under the directory src/main/resources/ I have two folders which contain static files. I'm trying to use those files but the program seems to think the folders are empty.

Here is some code I wrote to test the directories and see whether or not they contain any files.

    pathToFiles = "/resources/";

    if(true)
    {

        File kmlDirectory = new File(pathToFiles + "waitingKML/");

        Collection<File> files;
        files = FileUtils.listFiles(
                kmlDirectory,
                new RegexFileFilter("^(.*?)"),
                DirectoryFileFilter.DIRECTORY
        );

        if(files.isEmpty()) {

            return "Files are empty: " + kmlDirectory.toString();
        }
        else
            return "Files are NOT empty";
    }

And here is a picture of my directory structure (viewed from Netbeans).

enter image description here

When I run this test code I get 'Files are empty: \resources\waitingKML'.

enter image description here

According to documentation found here, https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot, Spring Boot will automatically add static web resources located within any of the following directories:

I'm wondering if this has to do with any property files or my configuration/setup?

Upvotes: 0

Views: 1579

Answers (1)

Scott Sosna
Scott Sosna

Reputation: 1413

It's all dependent on what the programs current working directory is. Often in an IDE, it's going to be the root of your project, not src/main, so it can't find it.

Once it's a deployed WAR, you need to ensure resources are packaged correctly (can't tell what you're using to package everything up) and instead of raw file access need to use something like ClassLoader.getResourceAsStream() to get the static files

Upvotes: 1

Related Questions