SpringBoot通过配置内置Tomcat托管本地静态文件、文件夹

SpringBoot已经内置了comcat,我们通过配置服务器本地文件夹路径到URL的映射即可通过配置好的URL访问文件夹中的文件资源。

1.首先,创建 WebMvcConfig 类,继承 WebMvcConfigurer

2.重写 addResourceHandlers方法;

registry.addResourceHandler("/photovoltaic/**").addResourceLocations("file:" + ConfigInfo.datasrcDir + "/");

其中:

"/photovoltaic/**" :访问时的IP+端口+/photovoltaic/...
"file:" + ConfigInfo.datasrcDir + "/" :ConfigInfo.datasrcDir为本地需要映射的文件夹,注意"file:"不能缺少,该参数表明是本地文件;

完整代码如下:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/photovoltaic/**").addResourceLocations("file:" + ConfigInfo.datasrcDir + "/");
        registry.addResourceHandler("/photovoltaictmp/**").addResourceLocations("file:" + ConfigInfo.datatempDir + "/");

        System.out.println("photovoltaic: " + "file:" + ConfigInfo.datasrcDir);
        System.out.println("photovoltaictmp: " + "file:" + ConfigInfo.datatempDir);
    }
}

示例:

启动时,我本地映射如下:

 

本地文件路径为:

D:\install\Tomcat\webapps\photovoltaic\datasrc\test\test.jpg

对应使用URL访问即为:

http://192.168.200.62:8085/photovoltaic/test/test.jpg

此时,浏览器可以正常访问:

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Springboot使用内置tomcat作为默认的web容器,无需额外安装Tomcat,只需在服务部署时直接启动jar包即可。这是因为在spring-boot-starter-web中,默认导入的是tomcat,所以启动时使用的web容器就是tomcat。如果你想配置使用其他的web容器,比如Undertow,可以按照以下步骤进行操作: 1. 修改pom.xml文件,排除默认引入的spring-boot-starter-tomcat依赖,并添加Undertow依赖: ```xml <dependencies> <!-- 导入web场景启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 排除tomcat starter --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- 引入undertow --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> </dependencies> ``` 通过这样的配置,你就可以使用Undertow作为springboot内置web容器了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot如何使用内嵌Tomcat](https://blog.csdn.net/u011523825/article/details/126114624)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot内置Tomcat配置和切换](https://blog.csdn.net/weixin_39158966/article/details/129745702)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luoyongweis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值