SpringBoot读取Resource下文件

一、前言
有时在项目中,涉及一些导入的功能,那么需要提供一些模板下载,那么就需要读取模板,而模板放在resource下,那么可以通过多种方式读取文件。

二、实现方式
1.通过 T.class.getClassLoader().getResourceAsStream() 方法,获取流。
读取方法:

public class ResourceUtils {

    public void getResourceFile(String fileName) throws IOException{
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
        readFileContent(in);
    }
	 public void readFileContent(Object obj) throws IOException {
        if (null == obj) {
            throw new RuntimeException("参数为空");
        }
        BufferedReader reader = null;
        // 如果是文件路径
        if (obj instanceof String) {
            reader = new BufferedReader(new FileReader(new File((String) obj)));
            // 如果是文件输入流
        } else if (obj instanceof InputStream) {
            reader = new BufferedReader(new InputStreamReader((InputStream) obj));
        }
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
    }

    public static void main(String[] args) throws IOException {
        new ResourceUtils().getResourceFile("template/test.txt");
    }

}

2.第二种:通过 T.class…getResourceAsStream() 方法。

public void getResourceTwo(String fileName) throws IOException{
    InputStream in = this.getClass().getResourceAsStream("/" + fileName);
    readFileContent(in);
}

public static void main(String[] args) throws IOException {
    new ResourceUtils().getResourceTwo("template/test.txt");
}

此方法默认是从 classpath 路径(即:src 或 resources 路径下)下查找文件的,但它的路径前需要加 “/” 。
这个是跟要读取的文件与当前.class 文件的位置有关。
相对于当前类 ResourceUtils,路径前是不需要加 “/”
相对于项目名(即:编译后的 classes 文件夹),路径前是需要加 “/”。

3.第三种:通过 ClassPathResource 方法。

public void getResourceThree(String fileName) throws IOException{
    ClassPathResource classPathResource = new ClassPathResource(fileName);
    readFileContent(classPathResource.getInputStream());
}

public static void main(String[] args) throws IOException {
    new ResourceUtils().getResourceThree("template/test.txt");
}

这种方式path 前加不加 “/” 无所谓。

4.第四种:使用ResourceLoader接口。
ResourceLoader接口是Spring框架提供的用于加载各种资源的接口,包括classpath下的资源。在Spring Boot中,可以通过依赖注入ResourceLoader接口来获取resources目录下的文件。

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

@Component
public class MyResource {
    
    private final ResourceLoader resourceLoader;
    
    public MyResource(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
    
    public void getResourceFile() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:my-file.txt");
        InputStream inputStream = resource.getInputStream();
        // 对文件进行操作,比如读取内容等
        readFileContent(in);
    }
}

以上是常见的读取方式。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜空下的星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值