springboot-静态资源配置(自定义容器)

静态资源路径是指系统可以直接访问的路径,且路径下的所有文件均可被用户直接读取。

在Springboot中默认的静态资源路径有:classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public/,从这里可以看出这里的静态资源路径都是在classpath中(也就是在项目路径下指定的这几个文件夹)

试想这样一种情况:一个网站有文件上传文件的功能,如果被上传的文件放在上述的那些文件夹中会有怎样的后果?

  • 网站数据与程序代码不能有效分离;
  • 当项目被打包成一个.jar文件部署时,再将上传的文件放到这个.jar文件中是有多么低的效率;
  • 网站数据的备份将会很痛苦。

此时可能最佳的解决办法是将静态资源路径设置到磁盘的基本个目录。

1、在Springboot中可以直接在配置文件中覆盖默认的静态资源路径的配置信息:

  • application.properties配置文件如下:
  • ########################################################
    ###server
    ########################################################
    server.port = 8081
    server.context-path=/springboot
    web.upload-path=F:/resource/
    spring.mvc.static-path-pattern=/**
    spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,\
    classpath:/static/,classpath:/public/,file:${web.upload-path}
    注意:web.upload-path这个属于自定义的属性,指定了一个路径,注意要以/结尾;
  • spring.mvc.static-path-pattern=/**表示所有的访问都经过静态资源路径;

  • spring.resources.static-locations在这里配置静态资源路径,前面说了这里的配置是覆盖默认配置,所以需要将默认的也加上否则staticpublic等这些路径将不能被当作静态资源路径,在这个最末尾的file:${web.upload-path}之所有要加file:是因为指定的是一个具体的硬盘路径,其他的使用classpath指的是系统环境变量

  • 测试1:在配置的web.upload-path路径下存放一张照片default.jpg,访问:http://127.0.0.1:8081/springboot/default.jpg地址照片显示配置成功。

  • 测试2:

  • package com.zslin;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.util.FileCopyUtils;
    
    import java.io.File;
    
    /**
     * Created by 钟述林 393156105@qq.com on 2016/10/24 0:44.
     */
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class FileTest {
    
        @Value("${web.upload-path}")
        private String path;
    
        /** 文件上传测试 */
        @Test
        public void uploadTest() throws Exception {
            File f = new File("D:/pic.jpg");
            FileCopyUtils.copy(f, new File(path+"/1.jpg"));
        }
    }

    注意:这里将D:/pic.jpg上传到配置的静态资源路径下,下面再写一个测试方法来遍历此路径下的所有文件。

  • @Test
    public void listFilesTest() {
        File file = new File(path);
        for(File f : file.listFiles()) {
            System.out.println("fileName : "+f.getName());
        }
    }

    可以到得结果:

    fileName : 1.jpg

    说明文件已上传成功,静态资源路径也配置成功。

    • 浏览器方式验证

    由于前面已经在静态资源路径中上传了一个名为1.jpg的图片,也使用server.port=8081设置了端口号为8081,所以可以通过浏览器打开:http://localhost:8081/1.jpg访问到刚刚上传的图片。

    原文链接地址
  • 2、代码实现静态资源配置
  • 如果你想增加如/mystatic/**映射到classpath:/mystatic/,你可以让你的配置类继承WebMvcConfigurerAdapter,然后重写如下方法:
  • @Configuration
    @ComponentScan
    public class WebConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
             registry.addResourceHandler("/mystatic/**").addResourceLocations("classpath:/mystatic/");
            }
    }
    
    
    @Configuration
    @ComponentScan
    public class WebConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
             registry.addResourceHandler("/mystatic/**").addResourceLocations("file:F:/mystatic/");
            }
    }
    
    这种方式会在默认的基础上增加/mystatic/**映射到classpath:/mystatic/,不会影响默认的方式,可以同时使用。
    测试地址:http://127.0.0.1:8081/springboot/mystatic/default.jpg地址照片显示配置成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值