spring boot访问本地静态资源

介绍

第一种方式
application.xml中加入如下配置
static-locations为本地资源路径
linux以/表示根目录,windows下可以用file:D来映射
在applicatiion.yaml文件中加入如下内容

spring:
  mvc:
    static-path-pattern: /**
  resources:
    static-locations: file:D:/image/

第二种方式
和上面其实差不多,配置方式改为java

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("file:D:/image/");
    }
}

演示

我把静态资源都放在D:/image这个目录下

各种本地资源都能展示
图片
在这里插入图片描述
pdf
在这里插入图片描述
视频
在这里插入图片描述

参考博客

[1]https://www.jianshu.com/p/217b1efd2a3c
[2]https://blog.csdn.net/yili0000a/article/details/82021165

猜你喜欢

转载自blog.csdn.net/zzti_erlie/article/details/92167589