Spring在webapps之外的磁盘上从外部文件夹获取文件和图像? [英] Spring get files and images from external folder on disk, outside webapps?
问题描述
我有webproject,其中包含src / main / webapp文件夹中的图像。我想将图像放在磁盘上的不同文件夹中。但我不知道如何管理访问此图像的请求。
我应该创建某种 httpServlet
,如下所示: http://balusc.omnifaces.org/2007/07/fileservlet.html
I have webproject which has images inside src/main/webapp folder. I would like to place images in different folder on the disk. But I have no idea how to manage requests to reach this images.
Should I create some kind of httpServlet
like shown here: http://balusc.omnifaces.org/2007/07/fileservlet.html
或者当我使用Spring MVC时java配置,有更合适和更简单的方法。
Or as I use Spring MVC with java configuration, there is more suitable and simpler way.
期待您的建议。
推荐答案
使用spring mvc对静态资源的支持,其位置
属性是一个Spring 资源
,所以你可以使用文件:
前缀
Use the spring mvc support for static resources, its location
attribute is an Spring Resource
, so you could use file:
prefix
<resources mapping="/resources/**" location="file:/my/external/directory/" />
或
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("file:/my/external/directory/");
}
}
@See Spring Reference Chapter 6.4 ResourceLoader for a列出所有资源前缀的表
@See Spring Reference Chapter 6.4 The ResourceLoader for a table that list all prefixed for resources
这篇关于Spring在webapps之外的磁盘上从外部文件夹获取文件和图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!