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
或者当我使用 Spring MVC 和 java 配置时,有更合适和更简单的方法。
期待您的建议。
使用springmvc support for static resources,它的location
属性是一个SpringResource
,所以你可以使用file:
前缀
<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/");
}
}
@请参阅 Spring Reference Chapter 6.4 The ResourceLoader 以获得 table,其中列出了资源的所有前缀
在我的例子中,我将 js 和 css 资源放在 webapp 中,将图像放在 e://images/
中。
对于这种情况,我使用两个 mvc:resources
映射
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:resources mapping="/images/**" location="file:e://images/"/>
并找到 e 的图像:> 图像 > abc.jpg 使用....
<img src="../images/abc.jpg"/>
你也可以试试<img src="/images/abc.jpg"/> or <img src="images/abc.jpg">
(如果不行)
链接在 webapp > resources > js > xyz.js 下的 css/js 如下所示。
<script type='text/javascript' src='../resources/js/xyz.js'></script>
您可以implement WebMvcConfigurer
在您的主应用程序或主应用程序中class。
@SpringBootApplication
@ComponentScan("com.your.package.controller")
@Configuration
public class ServicesApplication implements WebMvcConfigurer { // ServicesApplication is my main class
String your_drive_location = "file:///D:/upload_dir/upload/"; // my file path
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/**").addResourceLocations(your_drive_location );
}
public static void main(String[] args) {
SpringApplication.run(ServicesApplication.class, args);
}
}
我正在使用 JSTL 来展示:
<c:forEach items="${ListOfBrand}" var="brand"> // ListOfBrand is a Map
<img src=" <c:out value="/images/"/>${brand.brand_image}" /> // Showing all image from brand table
</c:forEach>
我有一个 webproject,它在 src/main/webapp 文件夹中有图像。我想将图像放在磁盘上的不同文件夹中。但我不知道如何管理访问这些图像的请求。
我是否应该像此处所示创建某种 httpServlet
:http://balusc.omnifaces.org/2007/07/fileservlet.html
或者当我使用 Spring MVC 和 java 配置时,有更合适和更简单的方法。
期待您的建议。
使用springmvc support for static resources,它的location
属性是一个SpringResource
,所以你可以使用file:
前缀
<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/");
}
}
@请参阅 Spring Reference Chapter 6.4 The ResourceLoader 以获得 table,其中列出了资源的所有前缀
在我的例子中,我将 js 和 css 资源放在 webapp 中,将图像放在 e://images/
中。
对于这种情况,我使用两个 mvc:resources
映射
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:resources mapping="/images/**" location="file:e://images/"/>
并找到 e 的图像:> 图像 > abc.jpg 使用....
<img src="../images/abc.jpg"/>
你也可以试试<img src="/images/abc.jpg"/> or <img src="images/abc.jpg">
(如果不行)
链接在 webapp > resources > js > xyz.js 下的 css/js 如下所示。
<script type='text/javascript' src='../resources/js/xyz.js'></script>
您可以implement WebMvcConfigurer
在您的主应用程序或主应用程序中class。
@SpringBootApplication
@ComponentScan("com.your.package.controller")
@Configuration
public class ServicesApplication implements WebMvcConfigurer { // ServicesApplication is my main class
String your_drive_location = "file:///D:/upload_dir/upload/"; // my file path
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/**").addResourceLocations(your_drive_location );
}
public static void main(String[] args) {
SpringApplication.run(ServicesApplication.class, args);
}
}
我正在使用 JSTL 来展示:
<c:forEach items="${ListOfBrand}" var="brand"> // ListOfBrand is a Map
<img src=" <c:out value="/images/"/>${brand.brand_image}" /> // Showing all image from brand table
</c:forEach>