通过SpringBoot 进行静态资源的访问:
@Configuration
public class ResourcesConfig extends WebMvcConfigurationSupport {
@Autowired
private RepeatSubmitInterceptor repeatSubmitInterceptor;
//后端拦截器-防止重复提交
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
super.addInterceptors(registry);
}
//addResoureHandler-对外暴露的访问路径
//addResourceLocations-文件放置的目录
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/profile/**")
.addResourceLocations("file:/Users/uploadPath/");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addResourceHandlers(registry);
}
}
方式连接示例:
https://demo.com/profile/wxuserfile/2020/05/21/2c38934fcf516c21f26be4c620e02ba4.png
映射服务器文件位置:/Users/uploadPath/wxuserfile/2020/05/21/2c38934fcf516c21f26be4c620e02ba4.png
当在项目中另设置 DispatcherServlet 需要对 DispatcherServlet 另命名,否则会出现冲突 导致其中一个失效。
如下:
@Bean
public ServletRegistrationBean servletRegistrationBean(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean bean = new ServletRegistrationBean(dispatcherServlet);
bean.addUrlMappings("*.do");
bean.setName("do_dispatcherServlet");
return bean;
}