Add External Resources Folder in Spring Download

I would like to add a resource folder regarding the location of the flag (in addition to the packed resources in my bank), for example:

/Directory
    Application.jar
    /resources
        test.txt

I tried the following:

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**")
            .addResourceLocations("/resources/", "file:/resources/");
}

I also tried:

.addResourceLocations("/resources/", "file:resources/");

Access to http://localhost:8080/resources/test.txtany setting results in a whitelabel error page. How can i solve this?

+8
source share
2 answers

Your second approach will work:

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**")
            .addResourceLocations("/resources/", "file:resources/");
}

but only if you started Spring Boot from /Directory, because file:resources/- this is a relative path.

cd Directory
java -jar Application.jar

, , , , .

+14
 @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry
            .addResourceHandler("/files/**")
            .addResourceLocations("file:/location1/", "file:/location2/");
}

, http://localhost: {port}/files/image.png

0

All Articles