How to use Spring Boot to serve static content located in Dropbox folder?

I have a Spring Boot web application and would like to show static content located in the Dropbox shared directory on my Linode VPS (~ / Dropbox / images). I read that Spring Boot will automatically load static content from

"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", 

but of course my Dropbox directory is not in the classpath.

Although I could configure Apache to work with images in my Dropbox folder, I would like to take advantage of Spring Security to restrict access to static content to authenticated users.

+61
spring-boot spring-mvc apache dropbox static-content
Jan 14 '14 at 20:33
source share
12 answers

You can add your own static resource handler (it overwrites by default), for example

 @Configuration public class StaticResourceConfiguration extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("file:/path/to/my/dropbox/"); } } 

There is some documentation on Spring Boot about this, but it's actually just a vanilla Spring MVC feature.

Also starting in spring 1.2 download (I think) you can just set spring.resources.staticLocations .

+65
Jan 14 '14 at
source share

Springboot (via Spring) now simplifies adding to existing resource handlers. See Dave Siers answer . To add static resources to existing handlers, just remember to use a resource handler path that does not override existing paths.

The two β€œyet” notes below remain valid.

. ,.

[Edit: the approach below is no longer valid]

If you want to extend the default static resource handlers, then something like this looks like:

 @Configuration @AutoConfigureAfter(DispatcherServletAutoConfiguration.class) public class CustomWebMvcAutoConfig extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { String myExternalFilePath = "file:///C:/Temp/whatever/m/"; registry.addResourceHandler("/m/**").addResourceLocations(myExternalFilePath); super.addResourceHandlers(registry); } } 

The call to super.addResourceHandlers sets the default handlers.

also:

  • Note the trailing slash in the external file path. (Depends on your expectation of matching URLs).
  • Consider viewing the source code for WebMvcAutoConfigurationAdapter .
+31
Nov 14 '14 at 21:46
source share

Based on @Dave Syers answer, add the following class to my Spring boot project:

 @Configuration public class StaticResourceConfiguration extends WebMvcConfigurerAdapter { private static final Logger LOG = LoggerFactory.getLogger(StaticResourceConfiguration.class); @Value("${static.path}") private String staticPath; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if(staticPath != null) { LOG.info("Serving static content from " + staticPath); registry.addResourceHandler("/**").addResourceLocations("file:" + staticPath); } } // see https://stackoverflow.com/questions/27381781/java-spring-boot-how-to-map-my-my-app-root-to-index-html @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("redirect:/index.html"); } } 

This allows me to run my Spring boot application with the --static.path as

 java -jar spring-app-1.0-SNAPSHOT.jar --static.path=/path/to/my/static-files/ 

This can be very convenient for development and testing.

+19
May 15 '15 at 10:14
source share

Here is the spring.resources.staticLocations property, which can be set in application.properties . Note that this will override the default location. See org.springframework.boot.autoconfigure.web.ResourceProperties .

+7
Nov 22 '15 at 5:59
source share

@ Mark Schaefer

It's never too late, but add a slash ( / ) after the static:

 spring.resources.static-locations=file:/opt/x/y/z/static/ 

So http://<host>/index.html now available.

+4
Mar 23 '18 at 18:46
source share

Based on @Dave Syer, @kaliatech and @asmaier respond that springboot v2 + will look like this:

 @Configuration @AutoConfigureAfter(DispatcherServletAutoConfiguration.class) public class StaticResourceConfiguration implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { String myExternalFilePath = "file:///C:/temp/whatever/m/"; registry.addResourceHandler("/m/**").addResourceLocations(myExternalFilePath); } } 
+3
Jan 16 '19 at 7:03
source share

Serve from file system

I added spring.resources.static-location=file:../frontend/build in application.properties

index.html present in the build folder

Usage can also add an absolute path.

spring.resources.static-location=file: /User/XYZ/Desktop/frontend/build

I think in a similar way you can try adding the path to the Dropbox folder.

+2
May 9 '18 at 11:21
source share

For the current version of Spring-Boot version 1.5.3, the parameter

spring.resources.static-locations

Update I configured

`spring.resources.static-locations = File: / OPT / x / y / g / static``

and expect my index.html to live in this folder when called

http://<host>/index.html

This did not work. I needed to specify the folder name in the URL:

http://<host>/static/index.html

+1
Jun 02 '17 at 10:41
source share

FWIW, I had no success with spring.resources.static-locations recommended above; It worked for me that I installed spring.thymeleaf.prefix:

 report.location=file:/Users/bill/report/html/ spring.thymeleaf.prefix=${report.location} 
+1
May 31 '18 at 20:20
source share

Note that WebMvcConfigurerAdapter is deprecated (see WebMvcConfigurerAdapter ). Due to the default Java 8 methods, you only need to implement WebMvcConfigurer .

+1
Jun 15 '18 at 9:31
source share
  • OS: Win 10
  • Spring Boot: 2.1.2

I wanted to serve static content from c: / images

Adding this property worked for me:

 spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:///C:/images/ 

I found the initial value of the property in the Spring A Boot Doc application , Appendix A

This will make c: /images/image.jpg available as http: // localhost: 8080 / image.jpg

+1
Apr 02 '19 at 20:22
source share

You can put your folder in the root of the ServletContext.

Then specify the relative or absolute path to this directory in application.yml:

 spring: resources: static-locations: file:some_temp_files/ 

Resources in this folder will be available (for example, for downloading) at:

 http://<host>:<port>/<context>/your_file.csv 
0
Sep 19 '19 at 16:19
source share



All Articles