SpringBoot读取Resource下文件的几种方式
# SpringBoot 读取 Resource 下文件的几种方式
# 方式一
ClassPathResource resource = new ClassPathResource("excel/test.xlsx");
InputStream file = resource.getInputStream();
1
2
2
# 方式二
InputStream inputStream = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("excel/test.xlsx");
1
2
3
2
3
# 方式三
InputStream inputStream = this.getClass()
.getResourceAsStream("excel/test.xlsx");
1
2
2
# 方式四
File file = ResourceUtils.getFile("classpath:excel/test.xlsx");
InputStream inputStream = new FileInputStream(file);
1
2
2
# 学习参考
SpringBoot读取Resource下文件的几种方式 - 简书 (jianshu.com) (opens new window)
上次更新: 2024/9/25 11:16:13