springboot静态资源访问

springboot的项目中,默认的开启的静态资源目录有:

     classpath:/META-INF/resources/,
     classpath:/resources/,
     classpath:/static/

设置自定义的静态资源目录有如下两种方式:

  1.基于javaconfig的注入    

@Component
public class WebConfigurer extends WebMvcConfigurerAdapter {

    @Value("${images-file}")
    String imagesFile;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/upload/images/**").addResourceLocations(imagesFile);
        super.addResourceHandlers(registry);
    }
}

  继承WebMvcConfigurerAdapter 并重写addResourceHandlers方法,这几定义多个静态资源

2.配置文件application.yml配置静态资源路径:

spring:
   resources:
     static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,file:${image-path}
image-path: D:/file/upload/

static-locations配置中,如果是本地目录,前面加file

你可能感兴趣的:(SpringBoot系列)