Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring code config addResourceLocations on disk

I'm saving files to a temp directory located outside of my project and I need to be able to link to them after I save them.

I tried adding a resource Handler

registry.addResourceHandler("/photo/**").addResourceLocations("D://photo//");

but it doesn't seem like spring understands that the file isn't on the classpath

21:58:48.293 [http-nio-8080-exec-14] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Mapping [/photo/a.png] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/D://photo//]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@782ce27b]]] and 1 interceptor

is there a prefix or something I'm missing?

Thanks!

like image 509
jgreenberg Avatar asked Dec 18 '14 03:12

jgreenberg


People also ask

What is a classpath in spring boot?

Where is the classpath in spring boot? It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable .


1 Answers

You can define absolute path in resources as given below.

registry.addResourceHandler("/test/**")
    .addResourceLocations("file:///Users/testuser/test/");

This example is for *nix, Use proper windows location after file://

Spring resource documentation is provided here

Another personal suggestion: I would try to reduce absolute file dependency as much as possible in any web application. Self contained project will be much more easier in maintenance point of view.

like image 82
kamoor Avatar answered Nov 10 '22 17:11

kamoor