springboot读取resources下文件的方式详解


Posted in Java/Android onJune 21, 2022

项目中很多时候需要读取自定义配置文件,本地开发工具怎么写都成功但是部署到服务其上就出现问题,

异常BOOT-INF/classes!/config.xml (文件名、目录名或卷标语法不正确.)路径中带有叹号之类的

了解了大概之后就是springboot打成jar是一个文件,也就是一个压缩包,没有办法读取压缩文件里的路径,因此要解决这个问题了解读取配置文件的原理,直接获取文件流就可以了。

springboot读取resources下文件的方式详解

1、使用项目内路径读取,只能在开发工具中使用,部署之后无法读取。(不通用

类似:src/main/resources/default.xml

File file = new File("src/main/resources/default.xml");

@Test
    public void testReadFile2() throws IOException {
        File file = new File("src/main/resources/default.xml");
        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();
    }

 2、使用org.springframework.util.ResourceUtils,读取。在linux环境中无法读取。(不通用)

File file = ResourceUtils.getFile("classpath:default.xml");
FileInputStream fis = new FileInputStream(file);

@Test
    public void testReadFile3() throws IOException {
        File file = ResourceUtils.getFile("classpath:default.xml");
        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();
    }

3、使用org.springframework.core.io.ClassPathResource,各种环境都能读取。(通用)

Resource resource = new ClassPathResource("resource.properties");
InputStream is = resource.getInputStream();

@Test
    public void testReadFile() throws IOException {
//        ClassPathResource classPathResource = new ClassPathResource("default.xml");
        Resource resource = new ClassPathResource("default.xml");
        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();
    }

4、结合spring注解,使用org.springframework.core.io.ResourceLoader;类的注解。(通用)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
 
    @Autowired
    ResourceLoader resourceLoader;
    
    
    @Test
    public void testReaderFile() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:default.xml");
        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();
    }
}

总结

到此这篇关于springboot读取resources下文件的文章就介绍到这了,更多相关springboot读取resources文件内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!


Tags in this post...

Java/Android 相关文章推荐
jackson json序列化实现首字母大写,第二个字母需小写
Jun 29 Java/Android
HashMap实现保存两个key相同的数据
Jun 30 Java/Android
SpringBoot集成Druid连接池连接MySQL8.0.11
Jul 02 Java/Android
dubbo集成zipkin获取Traceid的实现
Jul 26 Java/Android
Java比较两个对象中全部属性值是否相等的方法
Aug 07 Java/Android
idea以任意顺序debug多线程程序的具体用法
Aug 30 Java/Android
解析探秘fescar分布式事务实现原理
Feb 28 Java/Android
Java十分钟精通进阶适配器模式
Apr 06 Java/Android
JAVA长虹键法之建造者Builder模式实现
Apr 10 Java/Android
Android 中的类文件和类加载器详情
Jun 05 Java/Android
springboot读取resources下文件的方式详解
Jun 21 Java/Android
向Spring IOC 容器动态注册bean实现方式
Jul 15 Java/Android
java实现自定义时钟并实现走时功能
Jun 21 #Java/Android
SpringBoot使用ip2region获取地理位置信息的方法
Jun 21 #Java/Android
Android基础入门之dataBinding的简单使用教程
Jun 21 #Java/Android
一文搞懂Java中的注解和反射
Jun 21 #Java/Android
Android学习之BottomSheetDialog组件的使用
Jun 21 #Java/Android
SpringCloud中分析讲解Feign组件添加请求头有哪些坑梳理
Jun 21 #Java/Android
Mybatis-plus配置分页插件返回统一结果集
You might like
坏狼php学习 计数器实例代码
2008/06/15 PHP
php 获取完整url地址
2008/12/20 PHP
如何使用PHP计算上一个月的今天
2013/05/23 PHP
微信利用PHP创建自定义菜单的方法
2016/08/01 PHP
浅析php-fpm静态和动态执行方式的比较
2016/11/09 PHP
php与阿里云短信接口接入操作案例分析
2020/05/27 PHP
24款非常有用的 jQuery 插件分享
2011/04/06 Javascript
javascript中数组array及string的方法总结
2014/11/28 Javascript
Javascript中innerHTML用法实例分析
2015/01/12 Javascript
在JavaScript中如何解决用execCommand(
2015/10/19 Javascript
jquery.validate表单验证插件使用方法解析
2016/11/07 Javascript
JS实现双击内容变为可编辑状态
2017/03/03 Javascript
JavaScript输入分钟、秒倒计时技巧总结(附代码)
2017/08/17 Javascript
vue中如何去掉空格的方法实现
2018/11/09 Javascript
详解vue中axios请求的封装
2019/04/08 Javascript
JS实现简单的文字无缝上下滚动功能示例
2019/06/22 Javascript
一分钟学会JavaScript中的try-catch
2020/12/14 Javascript
[06:45]DOTA2卡尔工作室 英雄介绍幻影长矛手篇
2013/07/12 DOTA
[03:09]DOTA2亚洲邀请赛 LGD战队出场宣传片
2015/02/07 DOTA
python使用cookielib库示例分享
2014/03/03 Python
Python socket C/S结构的聊天室应用实现
2014/11/30 Python
Python中map和列表推导效率比较实例分析
2015/06/17 Python
python利用OpenCV2实现人脸检测
2020/04/16 Python
Python设计模式之MVC模式简单示例
2018/01/10 Python
Python 查看文件的读写权限方法
2018/01/23 Python
django获取from表单multiple-select的value和id的方法
2019/07/19 Python
Python turtle画图库&&画姓名实例
2020/01/19 Python
英国第一独立滑雪板商店:The Snowboard Asylum
2020/01/16 全球购物
大学生职业规划论文
2014/01/11 职场文书
人力资源主管的岗位职责
2014/03/15 职场文书
体育专业大学生职业生涯规划范文:打造自己的运动帝国
2014/09/12 职场文书
办理房产过户的委托书
2014/09/14 职场文书
工作自我推荐信范文
2015/03/25 职场文书
学术会议开幕词
2016/03/03 职场文书
Python 如何解决稀疏矩阵运算
2021/05/26 Python
教你使用Jenkins集成Harbor自动发布镜像
2022/04/03 Servers