SpringBoot读取Resources下文件
问题:
需要读取resources下的文件,文件格式不定,这里以txt为例,主要说明路径问题:
一、使用项目内路径读取,该路径只在开发工具中显示,类似:src/main/resources/resource.properties。只能在开发工具中使用,部署之后无法读取。(不通用)
主要代码:
File file = new File("src/main/resources/downloadApp.txt");
@Testpublic void testReadFile2() throws IOException {File file = new File("src/main/resources/downloadApp.txt");FileInputStream fis = new FileInputStream(file);InputStreamReader isr = new InputStreamReader(fis);BufferedReader br = new BufferedReader(isr);String data = null;while((data = br.readLine()) != null) {System.out.println(data);}br.close();isr.close();fis.close();}
二、使用org.springframework.util.ResourceUtils,读取。在linux环境中无法读取。(不通用)
主要代码:
File file = ResourceUtils.getFile("classpath:downloadApp.txt");FileInputStream fis = new FileInputStream(file);
@Testpublic void testReadFile3() throws IOException {File file = ResourceUtils.getFile("classpath:downloadApp.txt");FileInputStream fis = new FileInputStream(file);InputStreamReader isr = new InputStreamReader(fis);BufferedReader br = new BufferedReader(isr);String data = null;while((data = br.readLine()) != null) {System.out.println(data);}br.close();isr.close();fis.close();}
三、使用org.springframework.core.io.ClassPathResource,各种环境都能读取。(通用)
主要代码:
Resource resource = new ClassPathResource("downloadApp.txt");InputStream is = resource.getInputStream();
@Testpublic void testReadFile() throws IOException {Resource resource = new ClassPathResource("downloadApp.txt");InputStream is = resource.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);String data = null;while((data = br.readLine()) != null) {System.out.println(data);}br.close();isr.close();is.close();}
四、结合spring注解,使用org.springframework.core.io.ResourceLoader;类注解。(通用)
@RunWith(SpringRunner.class)
@SpringBootTest
public class EttpCustomApplicationTests {@AutowiredResourceLoader resourceLoader;@Testpublic void testReaderFile() throws IOException {Resource resource = resourceLoader.getResource("classpath:downloadApp.txt");InputStream is = resource.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);String data = null;while((data = br.readLine()) != null) {System.out.println(data);}br.close();isr.close();is.close();}}
PS:
使用了以上方式仍然读取不到resources包下的文件时,建议将打好的jar包反编译下,看下代码是否成功将资源文件打入到jar包内,如果没有打入到jar包,则不是读取方式的问题,需要找找为何未打入jar包的原因,这里贴一个笔者遇到的一个坑,希望能帮到大家~~
以前的大佬在pom文件中配置了打包时允许打入jar包的资源包下的文件类型,笔者开始就是因为没有放开txt类型文件,所以未打入到jar包中,导致读取失败的,本地也会出现,因为本地编译时,读取的也是字节码文件里的文件,即target包里面的文件,而不是java包下的文件
SpringBoot读取Resources下文件相关推荐
- springboot读取resources下文件方式
项目中很多时候需要读取自定义配置文件,本地开发工具怎么写都成功但是部署到服务其上就出现问题, 异常BOOT-INF/classes!/config.xml (文件名.目录名或卷标语法不正确.)路径中带 ...
- java读取Resources下文件
java读取Resources下文件_杰子的世界-CSDN博客_java获取resources下的文件 第四种, 读取路径 ResourceBundle bundle = ResourceBundle ...
- java 读取resources_java读取Resources下文件
java读取Resources下文件 第一种,读为流文件 InputStream path = this.getClass().getResourceAsStream("/data.txt& ...
- springboot读取src下文件_java(包括springboot)读取resources下文件方式
1.使用项目内路径读取,该路径只在开发工具中显示,类似:src/main/resources/resource.properties.只能在开发工具中使用,部署之后无法读取. File file = ...
- java(包括springboot)读取resources下文件方式
方式1 使用项目内路径读取,该路径只在开发工具中显示,类似:src/main/resources/resource.properties.只能在开发工具中使用,部署之后无法读取.(不通用) File ...
- springboot打包后读取resources下文件
pom.xml 配置 <build><resources><resource><directory>src/main/resources</dir ...
- springboot读取src下文件_springboot 下载模板文件 加载classpath文件路径(示例代码)...
项目需要下载导入模板,springboot使用如下下载方式可解决问题: /** * 导入模板下载 * @param req * @param resp * @param context * @retu ...
- springboot读取src下文件_springboot获取src/main/resource下的文件
问题如下: maven构建的springboot工程下的,文件路径 希望web端能够下载这里的"assess_remplate.docx"文件. 解决: 1.通过resource获 ...
- springboot打成jar后获取resources下文件失败, cannot be resolved to absolute file path because it does not resid
读取resources下的文件quotaShow.jasper 本地开发环境能正常下载: ClassPathResource resource = new ClassPathResource(&quo ...
最新文章
- 什么是CS/BS(一)转
- 加拿大第二大行TD Bank是如何践行科技战略的?
- 图片报错,显示默认图片
- Java并发:Callable、Future和FutureTask
- 理解Linux高性能网络架构的那些事
- # ALPHACAM 橱柜门玻璃门反面加工插件
- PINN内嵌物理知识神经网络入门及文献总结
- Threejs HDR
- 手把手教你用python几行代码打造人工智能对话机器人,还说说话!
- mac pdf去水印_PDF水印工具for Mac-PDF水印工具Mac版下载 V1.7-PC6苹果网
- android获取手机流量使用情况
- ACM-ICPC 2018 沈阳赛区网络预赛 J Ka Chang 分块
- Elasticsearch时区问题
- 3-1、React-Router基础使用plus
- (二)操作系统的发展与分类
- 乐山市计算机学校市技能大赛,乐山市第10届中职学校技能大赛开赛
- Azure学习笔记1.——三种网络流量分配的方法
- 线性代数学习笔记——第七十三讲——实对称矩阵的特征值与特征向量
- H5 新增那些特性:
- 如何用会声会影制作简约的倒计时片头
热门文章
- 6、Spring MVC 之 定义@RequestMapping处理方法
- 解决mysql的配置文件my.ini不起作用
- es html标签,Elasticsearch如何使用同义词搜索富文本html标签过滤以及分权限过滤搜索结果...
- 一物一码(6): 一物一码之【开展一物一码营销活动基本流程2,设计开发部署,生产包装,活动上线】
- 如何为Mac找到最合适的下载器
- ps照片处理器怎么打文字_什么是文字处理器?
- hashmap中的key是有序的么_关于HashMap中KEY的有序排列的反思和总结(对应TreeMap)
- kubeadm初始化集群报错:kubelet driver: “cgroupfs“ is different from docker cgroup driver: “systemd“
- vue使用addrouter添加动态路由
- 谷歌搜索引擎_Google推出搜索引擎建议