• 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读取资源文件
  • 相关阅读:
    Agent 与对象的辨析
    UI 自动化测试框架:PO 模式+数据驱动 【详解版】
    eclipse配置tomcat详解(图文版)
    H2创建表带注释的语法
    XML的创建和读取
    React报错之Type ‘() => JSX.Element[]‘ is not assignable to type FunctionComponent
    了解策略模式
    JAVA实训项目之学生管理系统(JDBC+GUI)
    初次使用IntelliJ IDEA从零开始学习创建maven项目
    JAVA面试
  • 原文地址:https://blog.csdn.net/feyehong/article/details/126334969