SpringBoot中静态资源访问配置以及内置tomcat虚拟文件映射路径是怎样的
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,本篇文章为大家展示了SpringBoot中静态资源访问配置以及内置tomcat虚拟文件映射路径是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。静态资源访
千家信息网最后更新 2025年11月14日SpringBoot中静态资源访问配置以及内置tomcat虚拟文件映射路径是怎样的
本篇文章为大家展示了SpringBoot中静态资源访问配置以及内置tomcat虚拟文件映射路径是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
静态资源访问配置
@Configuration@EnableWebMvcpublic class StaticResourceConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); }}内置tomcat虚拟文件映射路径
@Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/image/**").addResourceLocations("file:C:/image/"); }}应用场景
把文件独立出来放到其他盘符,比如我的服务在 /server 文件放到 /data 中
通常情况下放到对应项目的 webapps 下的文件才能正常访问,现在我需要访问E盘中的内容,这时候我就需要 自定义静态资源映射
实现方法
实现类继承 WebMvcConfigurerAdapter 并重写方法 addResourceHandlers,代码如下
import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic class FileServerConfig extends WebMvcConfigurerAdapter { @Value("${local.fileserver.dir}") private String localFileServerDir; @Value("${local.fileserver.path}") private String localFileServerPath; //访问图片方法 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/" + this.getLocalFileServerPath() + "/**").addResourceLocations("file:" + this.getLocalFileServerDir() + "/"); // 本地文件夹要以"flie:" 开头,文件夹要以"/" 结束,example: //registry.addResourceHandler("/abc/**").addResourceLocations("file:D:/pdf/"); super.addResourceHandlers(registry); } /** * 文件实际路径转为服务器url路径 * @param absolutePath * @return */ public String toServerPath(String absolutePath) { absolutePath = absolutePath.replaceAll("\\\\", "/"); return "/" + absolutePath.replace(localFileServerDir, localFileServerPath); } public String getLocalFileServerDir() { return localFileServerDir; } public void setLocalFileServerDir(String localFileServerDir) { this.localFileServerDir = localFileServerDir; } public String getLocalFileServerPath() { return localFileServerPath; } public void setLocalFileServerPath(String localFileServerPath) { this.localFileServerPath = localFileServerPath; }}注意:本地文件夹要以"flie:" 开头,文件夹要以"/" 结束,example:
registry.addResourceHandler("/pdf/**").addResourceLocations("file:D:/pdf/");
意思是你的服务器路径 serverdata 对应你本地的 /data目录这时你的,假如要访问 /data/test.jpg图片,对应的服务器路径为 http://localhost:serverdata/test.jpg
上述内容就是SpringBoot中静态资源访问配置以及内置tomcat虚拟文件映射路径是怎样的,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
文件
路径
资源
静态
文件夹
服务
配置
内容
方法
服务器
图片
开头
技能
知识
简明
简明扼要
代码
场景
实际
就是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
中国高校教师调查数据库
app 如何请求数据库
网络安全老三样是什么
导航软件开发平台
戴尔r720服务器管理网卡
会计软件开发设计规模是
网络安全护卫队有哪些活动
购买的服务器报废处理及账务处理
山西运城直销软件开发公司
数据库扫描器
服务器安装360安全卫士
我的世界多人搜索不到服务器
华燕科技软件开发
几种数据库的sql注入环境搭建
阿里云服务器的安全组怎么配置
数据库事物设计阶段
rs422 软件开发接口
网络安全宣传周是每年什么时间
软件开发与销售税点
软件开发企业认证
js 连数据库
安卓网页服务器管理
gta5末日三服务器破解对不准
山东省济南联想服务器代理经销商
购买的服务器报废处理及账务处理
计算机网络技术网上教程
简述数据库系统组成
创建数据库文件的五个属性
最好的跨平台软件开发ide
惠州计算机网络安全信息协会