SpringBoot深入探究四種靜態(tài)資源訪問的方式
1.默認的靜態(tài)資源目錄
/static
/public
/resources
/META-INF/resources
動態(tài)資源目錄:/templates
2.resources靜態(tài)資源目錄圖片存放

3. 靜態(tài)資源訪問
3.1.通過路徑訪問靜態(tài)資源
http://localhost:8080/a.jpg
http://localhost:8080/b.jpg
http://localhost:8080/c.png
http://localhost:8080/d.jpg
3.2.通過配置類配置路徑訪問本地靜態(tài)資源
1.config
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//自定義路徑mypic, addResourceLocations指定訪問資源所在目錄
registry.addResourceHandler("/mypic/**").addResourceLocations("file:C:\\Users\\Administrator\\Desktop\\images1\\");
//自定義路徑webjars訪問,addResourceLocations映射該路徑下的資源,resourceChain資源鏈
// registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/").resourceChain(true);
}
}2.訪問結果展示
路徑:http://localhost:8080/mypic/huangshanpic.webp

3.3.通過配置文件配置路徑訪問靜態(tài)資源
(1).application.yml
web.pic-path=C:/Users/Administrator/Desktop/images1/
spring.mvc.static-path-pattern=/mypic/**
spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/public/,classpath:/static/,file:${web.pic-path}
web.pic-path:訪問路徑
spring.mvc.static-path-pattern:采用全部映射到mypic路徑的方式
spring.web.resources.static-locations:配置允許訪問的靜態(tài)資源目錄
(2).訪問路徑格式
http://localhost:8080/mypic/a.jpg
http://localhost:8080/mypic/b.jpg
http://localhost:8080/mypic/c.png
http://localhost:8080/mypic/d.jpg
http://localhost:8080/mypic/web.pic-path配置本地路徑下的圖片名稱
3.4.通過引入打包靜態(tài)資源的jar包形式訪問
(1).靜態(tài)資源打jar包
創(chuàng)建一個新的web工程,只存放靜態(tài)資源
1).pom.xml
<artifactId>WWebjarsdemo</artifactId>
<version>1.0</version>
<build>
<resources>
<resource>
<!--
directory 將該路徑下的資源(example/0.0.3/資源)打包
targetPath 成該路徑下存儲
-->
<directory>${project.basedir}/src/main/resources</directory>
<targetPath>${project.build.outputDirectory}/META-INF/resources/webjars</targetPath>
</resource>
</resources>
</build>2).靜態(tài)資源目錄結構

3).package點擊打包

4).install到本地倉庫

(2).主項目中引入依賴包
1).pom.xml
<!--導入依賴的自定義靜態(tài)資源webjars包-->
<dependency>
<groupId>com.openlab</groupId>
<artifactId>WWebjarsdemo</artifactId>
<version>1.0</version>
</dependency>
<!--為了不再管理版本號-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
<version>0.35</version>
</dependency>(3).路徑訪問
未引入webjars-locator-core的jar包:http://localhost:8080/webjars/example/版本號/huangshan.webp
引入webjars-locator-core的jar包:
http://localhost:8080/webjars/example/huangshan.webp
注意:如果主程序和引入打包的jar包靜態(tài)資源下具備相同的目錄結構,如:META-INF\resources\webjars\example\0.0.1\**,可能會出現(xiàn)路徑訪問失敗的情況。
解決方法:clean主程序項目,重新運行。
(4).訪問結果

到此這篇關于SpringBoot深入探究四種靜態(tài)資源訪問的方式的文章就介紹到這了,更多相關SpringBoot靜態(tài)資源訪問內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springmvc圖片上傳及json數(shù)據轉換過程詳解
這篇文章主要介紹了springmvc圖片上傳及json數(shù)據轉換過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10
Java類型轉換valueOf與parseInt區(qū)別探討解析
這篇文章主要為大家介紹了Java類型轉換valueOf與parseInt區(qū)別探討解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
springboot多環(huán)境配置方案(不用5分鐘)
這篇文章主要介紹了springboot多環(huán)境配置方案(不用5分鐘),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01
SpringBoot整合Redis實現(xiàn)消息發(fā)布與訂閱的示例代碼
能實現(xiàn)發(fā)送與接收信息的中間介有很多,比如:RocketMQ、RabbitMQ、ActiveMQ、Kafka等,本文主要介紹了Redis的推送與訂閱功能并集成Spring Boot的實現(xiàn),感興趣的可以了解一下2022-08-08

