news 2026/4/23 11:35:35

springboot项目读取 resources 目录下的文件的9种方式

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
springboot项目读取 resources 目录下的文件的9种方式

1. 使用 ClassLoader.getResourceAsStream() 方法

InputStream inputStream = getClass().getClassLoader().getResourceAsStream(“file.txt”);

2.使用 Class.getResourceAsStream() 方法

InputStream inputStream = getClass().getResourceAsStream(“/file.txt”);

3.使用 ResourceLoader 加载文件

@Autowired private ResourceLoader resourceLoader;

Resource resource = resourceLoader.getResource(“classpath:file.txt”); InputStream inputStream = resource.getInputStream();

4.使用 ResourceUtils 加载文件

File file = ResourceUtils.getFile(“classpath:file.txt”);

5. 使用 ApplicationContext 加载文件

@Autowired private ApplicationContext applicationContext;

Resource resource = applicationContext.getResource(“classpath:file.txt”); InputStream inputStream = resource.getInputStream();

6.使用 ServletContext 加载文件

@Autowired private ServletContext servletContext;

InputStream inputStream = servletContext.getResourceAsStream(“/WEB-INF/classes/file.txt”);

7. 使用 File System 加载文件

File file = new File(“src/main/resources/file.txt”); InputStream inputStream = new FileInputStream(file);

8. 使用 Paths 和 Files 加载文件

Path path = Paths.get(“src/main/resources/file.txt”); InputStream inputStream = Files.newInputStream(path);

9. 使用 ClassPathResource 加载文件 (springboot项目 读取resources 推荐使用)

ClassPathResource resource = new ClassPathResource(“file.txt”); InputStream inputStream = resource.getInputStream();

案例: 模拟springboot 装配bean

配置文件

package com.ldj.springboot.importbean.selector; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.io.ClassPathResource; import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.CollectionUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.List; /** * User: ldj * Date: 2024/8/24 * Time: 23:54 * Description: No Description */ public class MyBeanSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata annotationMetadata) { List<String> springBeanPaths = SpringFactoriesLoader.getSpringBeanPaths("spring.factories"); if (CollectionUtils.isEmpty(springBeanPaths)) { throw new RuntimeException("spring.factories文件 缺少配置!"); } return springBeanPaths.toArray(new String[0]); } } // 读取要注入容器的类所在路径的配置文件 class SpringFactoriesLoader { public static List<String> getSpringBeanPaths(String path) { List<String> classPaths = new LinkedList<>(); InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; InputStream inputStream = null; String str; try { ClassPathResource classPathResource = new ClassPathResource(path); inputStream = classPathResource.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); bufferedReader = new BufferedReader(inputStreamReader); while ((str = bufferedReader.readLine()) != null) { System.out.println(str); classPaths.add(str); } } catch (Exception e) { e.printStackTrace(); return null; } finally { try { if (inputStream != null) { inputStream.close(); } if (bufferedReader != null) { bufferedReader.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } } catch (IOException e) { e.printStackTrace(); } } return classPaths; } } package com.ldj.springboot.importbean; import com.ldj.springboot.importbean.config.ProductConfig; import com.ldj.springboot.importbean.config.StoreConfig; import com.ldj.springboot.importbean.selector.MyBeanSelector; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Import; @Import(value = {MyBeanSelector.class}) public class ImportBeanApplication { public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(ImportBeanApplication.class); System.out.println(annotationConfigApplicationContext.getBean(ProductConfig.class)); System.out.println(annotationConfigApplicationContext.getBean(StoreConfig.class)); } }

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 11:33:46

SharpKeys键盘重映射终极指南:5分钟打造你的专属输入神器

SharpKeys键盘重映射终极指南&#xff1a;5分钟打造你的专属输入神器 【免费下载链接】sharpkeys SharpKeys is a utility that manages a Registry key that allows Windows to remap one key to any other key. 项目地址: https://gitcode.com/gh_mirrors/sh/sharpkeys …

作者头像 李华
网站建设 2026/4/17 9:21:55

Zotero PDF预览插件终极指南:7个简单步骤实现高效文献管理

Zotero PDF预览插件终极指南&#xff1a;7个简单步骤实现高效文献管理 【免费下载链接】zotero-pdf-preview Preview Zotero attachments in the library view. 项目地址: https://gitcode.com/gh_mirrors/zo/zotero-pdf-preview 在学术研究过程中&#xff0c;快速预览P…

作者头像 李华
网站建设 2026/4/18 8:37:43

PDF-Extract-Kit实战:构建智能文档处理流水线

PDF-Extract-Kit实战&#xff1a;构建智能文档处理流水线 1. 引言&#xff1a;智能文档处理的现实挑战与PDF-Extract-Kit的价值 在科研、教育、出版和企业办公等场景中&#xff0c;PDF文档承载了大量结构化与非结构化信息。然而&#xff0c;传统方式提取PDF内容时面临诸多痛点…

作者头像 李华
网站建设 2026/4/20 19:11:16

PDF-Extract-Kit性能优化:提升PDF处理速度5倍的秘诀

PDF-Extract-Kit性能优化&#xff1a;提升PDF处理速度5倍的秘诀 1. 背景与挑战&#xff1a;PDF智能提取中的性能瓶颈 1.1 PDF-Extract-Kit工具箱的技术定位 PDF-Extract-Kit是由开发者“科哥”基于开源生态二次开发构建的一套PDF智能内容提取工具箱&#xff0c;集成了布局检…

作者头像 李华