如何在Springboot架构下设置能访问静态资源
一、创建工具类的包
首先需要在您的项目中创建一个工具包如config包
特别提醒:这必须是在Spring boot框架下进行,其他的本题主暂时还不明。。。
二、创建一个工具类
工具类的名称随便都可,只要能表明是访问静态资源的工具类就行。
然后在这个工具类中编写方法,主要代码如图:
@Configuration
public class WebAppConfigurer extends WebMvcConfigurationSupport {
//定位项目文件夹下的upload图片文件,作为静态资源目录
static final String UPL_PATH = System.getProperty("user.dir")+"/upload/";
static final String IMG_PATH = System.getProperty("user.dir")+"/images/";
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry){
//静态资源映射
registry.addResourceHandler("/upload/**")
.addResourceLocations("file:"+UPL_PATH);
super.addResourceHandlers(registry);
System.out.println(UPL_PATH);
registry.addResourceHandler("/images/**")
.addResourceLocations("file:"+IMG_PATH);
super.addResourceHandlers(registry);
System.out.println(IMG_PATH);
}
}
三、测试访问
这个工具类主要用于在web端直接访问图片
如图,这样就可以直接通过输入路径的方式直接访问图片了