• SpringBoot 读取项目中静态资源文件



    一. ClassPathResource

    import org.springframework.core.io.ClassPathResource;
    import java.io.File;
    import java.io.InputStream;
    
    public void run(String... args) throws Exception {
    
    	// 读取本地的文件
        String filePath = "/temp/A110120119/测试文件.text";
        ClassPathResource readFile = new ClassPathResource(filePath);
    
        // 获取文件对象
        File file = readFile.getFile();
        System.out.println(file.getName());
    
        // 获取文件流
        InputStream inputStream = readFile.getInputStream();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述


    二. DefaultResourceLoader

    import org.springframework.core.io.DefaultResourceLoader;
    import org.springframework.core.io.ResourceLoader;
    import org.springframework.util.FileCopyUtils;
    
    public void run(String... args) throws Exception {
    
    	// 本地静态资源路径
        String filePath = "/temp/A110120119/测试文件.text";
        ResourceLoader resourceLoader = new DefaultResourceLoader();
    
        // 读取本地静态资源
        File orgFile = resourceLoader.getResource(filePath).getFile();
        System.out.println("本地静态资源的文件名为:" + orgFile.getName());
        // 创建临时文件(此时为空)
        File tempFile  = File.createTempFile("拷贝测试文件", ".text");
    
        // 将本地静态资源内容复制到创建的临时文件中
        FileCopyUtils.copy(orgFile, tempFile);
    
        // 读取临时文件中的所有内容并打印
        Files.readAllLines(tempFile.toPath()).forEach(System.out::println);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述


    三. PathMatchingResourcePatternResolver

    PathMatchingResourcePatternResolver是一个Ant模式通配符的Resource查找器,可以用来查找类路径下或者文件系统中的资源。

    import org.springframework.core.io.Resource;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.core.io.support.ResourcePatternResolver;
    
    public void run(String... args) throws Exception {
    
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    
    	// 本地静态资源路径
        String filePath = "/temp/A110120119/测试文件.text";
        // 获取指定路径下的资源文件
        Resource resource = resourcePatternResolver.getResource(filePath);
        System.out.println(resource.getFilename());
        System.out.println("-----------------------------------------------");
    
        // 获取temp文件夹下,所有文件夹中以 .text 为后缀的所有文件
        Resource[] resources = resourcePatternResolver.getResources("/temp/**/*.text");
        for (Resource resourceFile : resources) {
            System.out.println(resourceFile.getFilename());
        }
        System.out.println("-----------------------------------------------");
    	
    	// 获取本地磁盘中的资源文件路径
        Resource osFile = resourcePatternResolver.getResource("E:/写真/jojo/下载.png");
        System.out.println(osFile.getFilename());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述


    四. ResourceUtils

    ❗❗❗在SpringBoot中尽量避免使用ResourceUtils读取资源文件。ResourceUtils.getFile()获取的是资源文件的绝对路径,当项目打包为jar或者war包之后部署,资源文件的绝对路径改变,因此会报错。

    import org.springframework.util.ResourceUtils;
    
    public void run(String... args) throws Exception {
    
    	// 本地静态资源路径
        String filePath = "/temp/A110120119/测试文件.text";
    
        File file = ResourceUtils.getFile(filePath);
        System.out.println(file.getName());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述


    参考资料

    1. Springboot 生产环境下读取Resource下的文件
    2. SpringBoot不要使用ResourceUtils读取资源文件
  • 相关阅读:
    Linux:1.部分基础指令
    U盘启动盘 制作Linux Ubuntu CentOS系统启动盘 系统安装
    mybatis的简单使用
    JSP开发环境搭建(Tomcat的安装和配置)
    QT中的OpenGLWidget
    oppok10pro和红米k50哪个值得买 两者配置对比
    ArcGIS JS自定义Accessor,并通过watchUtils相关方法watch属性
    深入讲解Netty那些事儿之从内核角度看IO模型(上)
    vue真实项目还原
    插入排序问题
  • 原文地址:https://blog.csdn.net/feyehong/article/details/126334969