当前位置: 首页 > news >正文

SpringBoot——静态资源访问的四种方式

1.默认的静态资源目录

    /static
    /public 
    /resources
    /META-INF/resources

动态资源目录:/templates

2.resources静态资源目录图片存放

3. 静态资源访问
3.1.通过路径访问静态资源
    http://localhost:8080/a.jpg
    http://localhost:8080/b.jpg
    http://localhost:8080/c.png
    http://localhost:8080/d.jpg

3.2.通过配置类配置路径访问本地静态资源

1.config

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {@Overridepublic 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.通过配置文件配置路径访问静态资源

(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:配置允许访问的静态资源目录

(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. 通过引入打包静态资源的jar包形式访问

(1).静态资源打jar包

创建一个新的web工程,只存放静态资源

1).pom.xml

 <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).静态资源目录结构

3). package点击打包

 4).install到本地仓库

(2).主项目中引入依赖包

1).pom.xml

        <!--导入依赖的自定义静态资源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包静态资源下具备相同的目录结构,如:META-INF\resources\webjars\example\0.0.1\**,可能会出现路径访问失败的情况。

解决方法:clean主程序项目,重新运行。

(4).访问结果

http://www.lryc.cn/news/464402.html

相关文章:

  • WPF中的Style如何使用
  • 数据分析案例-欺诈性电子商务交易数据集可视化分析
  • java互联网医院智能导诊系统源码,Uniapp前端开发框架,支持一次编写,多端运行
  • 公交线路查询web管理系统||公交线路查询|基于SprinBoot+vue公交线路查询系统(源码+数据库+文档)
  • AI对于智能网联汽车发展路径的演化的助力
  • linux java17 - linux环境 centos7卸载java8安装java17
  • 高中数学:立体几何-外接球的外心法
  • 【Python-AI篇】人工智能python基础-计算机组成原理
  • Java Exercise
  • 滚雪球学Redis[9.1讲]:Redis的常见问题与最佳实践
  • python获取当前鼠标位置的RGB值
  • Ubuntu20.04运行深蓝运动规划hw_5
  • 删除node_modules文件夹
  • 基于Springboot+Vue的民宿管理系统(含源码数据库)
  • [LeetCode] 542. 01矩阵
  • 国产AI模型“Yi-Lightning”逆袭超越GPT-4!
  • 安卓設備上怎麼設置HTTP代理?
  • 学习Redisson实现分布式锁
  • 2024CSP-J模拟赛9————S12678
  • HarmonyOS中ArkUi框架中常用的装饰器
  • 服务攻防之Redis数据库安全
  • 随机森林算法的原理与实现
  • 模仿百度-基础版
  • c++贴瓷砖
  • 用 Python 构建高级配对交易策略
  • Java 引用数据类型详解、字符串的不可变性、如何处理字符串的内存管理、String Pool 及其优化
  • Babel使用
  • 自动机器学习(AutoML)
  • Vivado时序报告六:Report Timing详解
  • java基础:数据类型的总结