Spring code config addResourceLocations on disk

I am saving files in a temporary directory located outside of my project, and I need to be able to reference them after saving.

I tried to add a resource handler

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

but spring doesn't seem to understand that the file is not in the classpath

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

is there a prefix or something that i'm missing?

Thanks!

+6
source share
1 answer

You can define the absolute path in resources as follows.

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

This example is for * nix, use the correct window layout after the file: //

Spring provides resource documentation here

Another personal suggestion: I would try to minimize the dependency of the absolute file in any web application. An independent project will be much easier in terms of maintenance.

+6
source

Source: https://habr.com/ru/post/979820/


All Articles