基于SpringBoot 的 文件 上传存储

本文介绍如何解决SpringBoot应用中照片等文件上传路径的问题。由于SpringBoot使用内嵌Tomcat并以插件方式启动,因此需要对静态资源路径进行特殊配置。通过自定义WebAppConfig类,设置静态文件的映射路径,确保文件可以正确上传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为 springBoot 是通过 插件 方式启动,spring boot 使用的是内嵌的tomcat, 而且spring boot 把静态的文件全部在启动的时候都会加载到classpath的目录下的,   不是 通过Tomcat 方式启动 所以 照片 等文件 上传路径 会出现问题

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
 * 本地 映射   E:/content/
 * @author wangmx 
 */
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/content/**").addResourceLocations("file:E:/content/");
        super.addResourceHandlers(registry);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值