SpringBoot 系列教程(十八):SpringBoot通过url访问获取内部或者外部磁盘图片

一、默认静态资源映射规则

1. 有5个默认的静态资源存放位置

在 Spring Boot 中,默认情况下,一共有5个位置可以放静态资源,Spring Boot 默认将 /** 所有访问映射到以下目录,五个路径分别是如下5个:

classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
"/" : 静态资源存放在当前项目的根路径下,放在这个根路径下也是可以访问的

 例如:

      (1)、在src/main/resources/目录下创建 META-INF/resources文件夹,存放a.jpg 图片文件      

      (2)、在src/main/resources/目录下创建resources文件夹 ,存放图片b.jpg  图片文件      

      (3)、在src/main/resources/目录下创建static文件夹 ,存放图片c.jpg 图片文件

      (4)、在src/main/resources/目录下创建 public文件夹 ,存放图片d.jpg 图片文件         

      (5)、在src/main/resources/根目录直接存放静态资源文件,存放图片e.jpg 图片文件         

总结: 上面这几个都是静态资源的映射路径,优先级顺序为:META-INF/resources > resources > static > public > /

2.配置application.yml的端口

# 开发环境配置
server:
  # 服务器的HTTP端口,默认为8080
  port: 8787

3.浏览器分别访问以下静态资源:

http://localhost:8787/a.jpg
http://localhost:8787/b.jpg
http://localhost:8787/c.jpg
http://localhost:8787/d.jpg
http://localhost:8787/e.jpg

均能正常访问相应的图片资源。那么说明,Spring Boot 默认会挨个从 public、 resources 、static目录里面找是否存在相应的资源,如果有则直接返回; 没有则会跳转到SpringBoot默认的404页面。

讲到这里,我们已经知道了上面5种都是SpringBoot框架官方约定成俗的静态资源默认存放的位置,以及默认的访问映射到静态资源文件的URL。 那么接下来我们了解下在SpringBoot中如何自定义静态资源映射。

二、自定义静态资源映射配置,访问静态资源文件

在实际开发中,可能需要自定义静态资源访问路径,我们有好几种做法可以实现,首先可以继承WebMvcConfigurerAdapter类来实现(注意: 在SpringBoot2.0及Spring 5.0 之后版本WebMvcConfigurerAdapter已被废弃)

首先我贴一张项目静态资源存放文件的图:

1. a.jpg图片存放在static默认的目录下面,访问:http://127.0.0.1:8787/a.jpg ,可以正确的访问到

2. b.jpg图片存放在static目录下的img目录里面,img目录就是根据需求我们自定义的一个存放静态文件的文件夹,访问: http://127.0.0.1:8787/img/b.jpg , 可以正常的访问到

通过上述测试,我们可以将静态资源访问分为以下两类:

1. 访问默认静态资源目录文件:

如果是访问默认静态资源目录下的文件,直接使用:https:127.0.0.1/8787/a.jpg即可;

2. 访问自定义文件夹存放静态资源的文件:

如果是访问以自定义文件夹形式存放静态资源的文件,则需要带上自定义的文件夹名称,这样一来虽然不用做任何配置,但是需要根据文件夹的名称来确定访问资源文件的URL,这样及其不方便,为了统一访问静态资源路径,有如下3种配置方式。

3. 在项目的static-img文件夹下放一个图片,我们就叫c.jpg

方式1:继承WebMvcConfigurerAdapter类实现静态资源配置

package com.thinkingcao.config;

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

/**
 * 配置静态资源映射
 * @author cao
 * @since 2019/01/23
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/img/");
    }
}

重启项目,访问静态资源:http://localhost:8787/static/c.jpg , 能正常访问static目录下的c.jpg图片资源。

方式2:在application.yml配置文件中配置(推荐)

在application.yml中添加配置,将所有静态资源文件访问都映射到classpath:/static/img 目录下:

spring:
  mvc:
    #资源文件的访问路径,例如:http://127.0.0.1:8787/static/20190123115705.jpg
    static-path-pattern: /static/**
  resources:
    #资源文件存放的位置
    static-locations: classpath:/static/img/

 

重启项目,访问静态资源:http://localhost:8787/static/c.jpg , 同样能正常访问static目录下的c.jpg图片资源。

注意:通过spring.mvc.static-path-pattern这种方式配置,会使Spring Boot的默认配置失效,也就是说/public 、/resources 等默认配置不能使用了

总结: 上述配置中配置了静态模式为/static/就只能通过/static/来访问。

方式3:自定义配置类实现WebMvcConfigurer接口(推荐)

@Configuration
public class AdminWebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 设置项目静态资源访问
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/img/");
    }

}

 

小结: 上面实现自定义静态资源映射的实现一共有3种方式,其目的都是相同的,那就是不管静态资源目录结构如何,我的访问前缀都是统一的,都是通过: http://localhost:8787/static/xx.jpg  的形式访问,上述3中方式我推荐方式二和方式三。

三、自定义外部静态资源映射,访问本地磁盘图片文件

想要实现自定义静态资源映射地址,访问某个磁盘上的图片文件,这里讲解4种实现方式

方式1: application.yml文件中配置 (推荐)

spring:
  mvc:
    #访问路径,例如: 127.0.0.1:8787/images/aa.jpg
    static-path-pattern: /images/**
  resources:
    #资源存放的磁盘位置
    static-locations: file:D://Images//壁纸/

  本地磁盘图片:

  

访问静态资源:http://127.0.0.1:8787/images/护眼模式_20190123110237.jpg

注意:

1. 如果yml中配置了项目访问前缀context-path, 请注意,访问路径需要加上前缀,别漏掉了,注意细节问题。

访问效果如下:

 

方式2: 继承WebMvcConfigurerAdapter 类

在代码上配置, 继承WebMvcConfigurerAdapter类,但是在SpringBoot2.0和Spring 5.0 WebMvcConfigurerAdapter及之后的版本中已被废弃,目前找到解决方案就有两种,请往下看:

  •  重写WebMvcConfigurerAdapter类 

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

/**
 * <pre>
 * @Description:配置静态资源映射
 * @Aouth: cao_wencao
 * @Date: 2019-01-23 16:11
 * </pre>
 */
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //addResourceHandler是指你想在url请求的路径
        //addResourceLocations是图片存放的真实路径
        registry.addResourceHandler("/images/**").addResourceLocations("file:D://Images//壁纸/");
        super.addResourceHandlers(registry);
    }

}
  • 说到这里,我列举一下WebMvcConfigurerAdapter 比较常用的需要重写的接口,后续用得着
/** 解决跨域问题 **/
public void addCorsMappings(CorsRegistry registry) ;
/** 添加拦截器 **/
void addInterceptors(InterceptorRegistry registry);
/** 这里配置视图解析器 **/
void configureViewResolvers(ViewResolverRegistry registry);
/** 配置内容裁决的一些选项 **/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/** 视图跳转控制器 **/
void addViewControllers(ViewControllerRegistry registry);
/** 静态资源处理 **/
void addResourceHandlers(ResourceHandlerRegistry registry);
/** 默认静态资源处理器 **/
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)

 

方式3:实现WebMvcConfigurer接口(推荐)

实现WebMvcConfigurer接口这种方式是新版本中替代WebMvcConfigurerAdapter 已过时的一种解决方案。

  • 直接实现WebMvcConfigurer接口(官方推荐使用这个)
package com.thinkingcao.config;

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

/**
 * <pre>
 * @Description:
 * @Aouth: cao_wencao
 * @Date: 2019-01-23 17:28
 * </pre>
 */
@Configuration
public class WebMvcConfig implements  WebMvcConfigurer {
  

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("file:D://Images//壁纸/");
    }
}

访问静态资源文件: http://127.0.0.1:8787/images/护眼模式_20191229130634.jpg  

效果图如下:

方式4 :继承WebMvcConfigurationSupport类

继承WebMvcConfigurationSupport类这种方式是新版本中替代WebMvcConfigurerAdapter 已过时的第二种解决方案。

  • 继承WebMvcConfigurationSupport类
package com.thinkingcao.config;

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

/**
 * <pre>
 * @Description:
 * @Aouth: cao_wencao
 * @Date: 2019-01-23 17:39
 * </pre>
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("file:D://Images//壁纸/");
    }
}

访问静态资源文件:http://127.0.0.1:8787/images/护眼模式_20191229130648.jpg

效果图如下:

 

  • 贴一下WebConfig继承WebMvcConfigurationSupport类的常用配置

package com.thinkingcao.config;

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

/**
 * <pre>
 * @Description:
 * @Aouth: cao_wencao
 * @Date: 2019-01-23 17:39
 * </pre>
 */

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {


    /**
     * 跨域支持
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600 * 24);
    }


    /**
     * 添加静态资源--过滤swagger-api (开源的在线API文档)
     *
     * @param registry
     */

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:D:/Images/壁纸/");
        //过滤swagger
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/swagger-resources/**")
                .addResourceLocations("classpath:/META-INF/resources/swagger-resources/");

        registry.addResourceHandler("/swagger/**")
                .addResourceLocations("classpath:/META-INF/resources/swagger*");

        registry.addResourceHandler("/v2/api-docs/**")
                .addResourceLocations("classpath:/META-INF/resources/v2/api-docs/");
    }

}

 

四、源码

源码: https://github.com/Thinkingcao/SpringBootLearning/tree/master/springboot-aop

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章