Spring从磁盘上的外部文件夹获取文件和图像,在webapps之外?

Edg*_*gar 4 java resources spring external

我有webproject,其中包含src/main/webapp文件夹中的图像.我想将图像放在磁盘上的不同文件夹中.但我不知道如何管理访问此图像的请求.我应该创建一些httpServlet类似于此处显示的内容:http://balusc.omnifaces.org/2007/07/fileservlet.html

或者当我使用Spring MVC和java配置时,有更合适和更简单的方法.

期待您的建议.

Ral*_*lph 7

使用spring mvc支持静态资源,它的location属性是Spring Resource,所以你可以使用file:前缀

<resources mapping="/resources/**" location="file:/my/external/directory/" />
Run Code Online (Sandbox Code Playgroud)

要么

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("file:/my/external/directory/");
    }
}
Run Code Online (Sandbox Code Playgroud)

@See Spring Reference Chapter 6.4用于列出所有资源前缀的表的ResourceLoader


Mr.*_*Mak 5

就我而言,我将js和CSS资源放置在webapp中,将图像放置在中e://images/。对于这种情况,我使用两个mvc:resources映射

<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:resources mapping="/images/**" location="file:e://images/"/>
Run Code Online (Sandbox Code Playgroud)

并使用... 找到e:> images> abc.jpg的图像。

<img src="../images/abc.jpg"/>
Run Code Online (Sandbox Code Playgroud)

您也可以尝试<img src="/images/abc.jpg"/> or <img src="images/abc.jpg">(如果不起作用)

如下所示,在webapp>资源> js> xyz.js下链接了css / js 。

<script type='text/javascript' src='../resources/js/xyz.js'></script>
Run Code Online (Sandbox Code Playgroud)