一. 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();
}

在这里插入图片描述


二. 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);
}

在这里插入图片描述


三. 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());
}

在这里插入图片描述


四. 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. Springboot 生产环境下读取Resource下的文件
  2. SpringBoot不要使用ResourceUtils读取资源文件
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐