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

在这里插入图片描述


二. 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读取资源文件
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值