天天看點

springboot配置直接通路圖檔

一、前言

在接觸springboot之前,通過tomcat配置的虛拟目錄來通路圖檔。或者通過nginx的靜态資源來通路。在使用springboot後,發現可以配置直接通路靜态資源。

二、application.yml增加配置

在application.yml中增加配置:

spring:
	resources:
      static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:/data/upload
           

三、配置攔截

在實作接口WebMvcConfigurer的類中,增加對靜态資源的映射

@Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
  registry.addResourceHandler("/upload/**").addResourceLocations("file:/data/upload");
  super.addResourceHandlers(registry);
 }
           

四、上傳圖檔

講圖檔放置到/data/upload目錄即可。

五、通路

http://ip:port/圖檔名稱