idea中使用springboot 添加访问图片或者其他的虚拟路径

所需步骤:(为了大家方便使用我将代码直接粘在下方)

1.新建一个类继承WebMvcConfigurerAdapter

2.重写addResourceHandlers方法

3.加入要配置的虚拟路径registry.addResourceHandler("/img/**").addResourceLocations("file:E:\\musicInfo\\img\\"); 

将下方代码放至SpringBoot项目中,

将("/img/**")改为你需要访问的路径,

将("file:E:\\musicInfo\\img\\")改成绝对路径,注意不要删路径前的 file:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //文件磁盘图片url 映射
    //配置server虚拟路径,handler为前台访问的目录,locations为files相对应的本地路径
        registry.addResourceHandler("/img/**").addResourceLocations("file:E:\\musicInfo\\img\\");
    }
}

以上就是我的解决方法亲测可用,后期如果遇到问题还会完善

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值