SpringBoot 靜態資源訪問問題(一)

通過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/hongkai/ZBKF/uploadPath/");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addResourceHandlers(registry);
    }

}

方式連接示例:

https://demo.com/profile/wxuserfile/2020/05/21/2c38934fcf516c21f26be4c620e02ba4.png

映射服務器文件位置:/Users/hongkai/ZBKF/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;
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章