SpringBoot:靜態資源的訪問和配置

默認靜態資源訪問

Spring Boot的默認靜態資源的路徑爲: 
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ 
優先級從從高到低。

SpringBoot中可以直接在配置文件中覆蓋默認的靜態資源路徑的配置信息:

  • application.properties配置文件如下:
    web.upload-path=E:/jsr_img/
    
    spring.mvc.static-path-pattern=/PHOTO/**
    spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}

     

注意:

 ①web.upload-path這個屬於自定義的屬性,指定了一個路徑,注意要以/結尾;

 ②spring.mvc.static-path-pattern=/PHOTO/**表示所有的訪問都經過靜態資源路徑;

 ③spring.resources.static-locations在這裏配置靜態資源路徑,前面說了這裏的配置是覆蓋默認配置,所以需要將默認的也加上,否則staticpublic等這些路徑將不能被當作靜態資源路徑,在這裏的最末尾加上file:${web.upload-path}之所以要加file:是因爲要在這裏指定一個具體的硬盤路徑,其他的使用classpath指定的是系統環境變量;

訪問圖片:

http://localhost:端口號/數據庫中的圖片路徑

其他配置文件:

spring.http.multipart.enabled=true #默認支持文件上傳.

spring.http.multipart.file-size-threshold=0 #支持文件寫入磁盤.

spring.http.multipart.location= # 上傳文件的臨時目錄

spring.http.multipart.max-file-size=1Mb # 最大支持文件大小

spring.http.multipart.max-request-size=10Mb # 最大支持請求大小

 

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