news 2026/9/2 21:26:43

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/9/1 14:48:51

Unity游戏资源提取完全指南:从入门到精通

Unity游戏资源提取完全指南&#xff1a;从入门到精通 【免费下载链接】AssetRipper GUI Application to work with engine assets, asset bundles, and serialized files 项目地址: https://gitcode.com/GitHub_Trending/as/AssetRipper 你是否曾经看到Unity游戏中的精美…

作者头像 李华
网站建设 2026/8/26 14:09:11

一文说清Keil中头文件无法包含的根本原因

为什么Keil总说“找不到头文件”&#xff1f;一文彻底讲透路径背后的真相你有没有遇到过这样的场景&#xff1a;明明看到stm32f4xx_hal.h就躺在项目文件夹里&#xff0c;结果一编译——fatal error: stm32f4xx_hal.h: No such file or directory瞬间懵了。重启Keil&#xff1f;…

作者头像 李华
网站建设 2026/9/2 20:14:46

Windows热键侦探:一键解决快捷键冲突的完整指南

Windows热键侦探&#xff1a;一键解决快捷键冲突的完整指南 【免费下载链接】hotkey-detective A small program for investigating stolen hotkeys under Windows 8 项目地址: https://gitcode.com/gh_mirrors/ho/hotkey-detective 当你按下熟悉的CtrlC准备复制文本&am…

作者头像 李华
网站建设 2026/9/2 11:04:01

系统权限管理工具技术解析与应用实践

系统权限管理工具技术解析与应用实践 【免费下载链接】JiYuTrainer 极域电子教室防控制软件, StudenMain.exe 破解 项目地址: https://gitcode.com/gh_mirrors/ji/JiYuTrainer 在数字化教学环境中&#xff0c;系统权限管理工具作为平衡教学控制与学习自主的关键技术解决…

作者头像 李华
网站建设 2026/9/2 20:14:43

JFlash驱动中Flash Bank管理操作指南

JFlash中的Flash Bank管理&#xff1a;从原理到实战的完整指南 在嵌入式开发的世界里&#xff0c;固件烧录早已不再是“一键下载”那么简单。随着系统复杂度飙升&#xff0c;尤其是汽车电子、工业控制和物联网设备对可靠性和可维护性的严苛要求&#xff0c;传统的整片擦写方式…

作者头像 李华
网站建设 2026/9/2 20:14:44

SpringBoot(7)-Swagger

目录 一、是什么 二、SpringBoot集成Swagger 三、配置Swagger 3.1 配置文档信息 3.2 配置扫描接口 3.3 配置Swagger开关 3.4 配置API分组 3.5 实体配置 四、常用注解 五、总结 一、是什么 是一款API框架&#xff0c;API文档和API定义同步更新&#xff0c;可以在线测…

作者头像 李华