弹簧代码在磁盘上配置addResourceLocations

jgr*_*erg 8 java spring spring-mvc

我将文件保存到项目外部的临时目录中,我需要能够在保存后链接到它们.

我尝试添加资源处理程序

registry.addResourceHandler("/photo/**").addResourceLocations("D://photo//");
Run Code Online (Sandbox Code Playgroud)

但是看起来spring并不认为文件不在类路径上

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
Run Code Online (Sandbox Code Playgroud)

是否有一个前缀或我缺少的东西?

谢谢!

kam*_*oor 8

您可以在资源中定义绝对路径,如下所示.

registry.addResourceHandler("/test/**")
    .addResourceLocations("file:///Users/testuser/test/");
Run Code Online (Sandbox Code Playgroud)

此示例适用于*nix,在file://之后使用正确的窗口位置

这里提供 Spring资源文档

另一个个人建议:我会尝试在任何Web应用程序中尽可能减少绝对文件依赖性.从维护的角度来看,自包含项目将更容易.