###項目中須要將圖片放在磁盤上,不能將圖片放在webapp下面!jquery
springboot默認配置基本上能夠知足咱們的平常須要 可是項目中大量用戶上傳的圖片,不能放在tomcat下面,這樣子每次從新部署項目的時候,圖片就失效了,非常麻煩。web
因此此時就須要自定義配置springboot的項目靜態文件映射spring
springboot默認的配置規則tomcat
映射 /** 到springboot
classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources
複製代碼
到本地文件路徑也就是 resource/static/ 下面 訪問時能夠: localhost:8080/+資源路徑+資源名bash
例如個人項目結構! mvc
此時我訪問的靜態資源爲:app
localhost:8080/js/jquery.min.jswebapp
若是配置 jquery.min.js 直接在static下面 訪問則是ide
localhost:8080/jquery.min.js
但如今須要自定義映射規則:
有兩種方法一種是基於配置文件,另外一種是基於代碼層面配置。
1 基於配置文件
#配置內部訪問地址和外部圖片訪問地址 /myimgs/**
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=file:C:/Users/tizzy/Desktop/img/,classpath:/static/
複製代碼
映射 /** 到 本地磁盤路徑下存放的圖片,和tomcat中的圖片路徑
訪問路徑則是
localhost:8080/jquery.min.js
localhost:8080/ 圖片名
複製代碼
2 基於代碼層面配置
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//addResourceHandler是指你想在url請求的路徑
//addResourceLocations是圖片存放的真實路徑
registry.addResourceHandler("/**").addResourceLocations("file:C:/Users/tizzy/Desktop/img/").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}
}
複製代碼