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 相关文章推荐
Java并发编程必备之Future机制
Jun 30 Java/Android
springboot临时文件存储目录配置方式
Jul 01 Java/Android
解决Swagger2返回map复杂结构不能解析的问题
Jul 02 Java/Android
logback 实现给变量指定默认值
Aug 30 Java/Android
关于springboot配置druid数据源不生效问题(踩坑记)
Sep 25 Java/Android
springboot layui hutool Excel导入的实现
Mar 31 Java/Android
Android超详细讲解组件ScrollView的使用
Mar 31 Java/Android
零基础学java之带返回值的方法的定义和调用
Apr 10 Java/Android
零基础学java之带参数以及返回值的方法
Apr 10 Java/Android
Spring Data JPA框架的核心概念和Repository接口
Apr 28 Java/Android
Springboot-cli 开发脚手架,权限认证,附demo演示
Apr 28 Java/Android
Java+swing实现抖音上的表白程序详解
Jun 25 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抓即时股票信息
2006/10/09 PHP
PHP最常用的2种设计模式工厂模式和单例模式介绍
2012/08/14 PHP
php去除数组中重复数据
2014/11/18 PHP
PHP中常用的数组操作方法笔记整理
2016/05/16 PHP
背景音乐每次刷新都可以自动更换
2007/02/01 Javascript
Jquery实战_读书笔记1—选择jQuery
2010/01/22 Javascript
js 禁用只读文本框获得焦点时的退格键
2010/04/25 Javascript
jquery动画1.加载指示器
2012/08/24 Javascript
jQuery prev ~ siblings选择器使用介绍
2013/08/09 Javascript
JavaScript数组常用方法
2015/03/02 Javascript
jQuery子窗体取得父窗体元素的方法
2015/05/11 Javascript
JavaScript条件判断_动力节点Java学院整理
2017/06/26 Javascript
Vue.js最佳实践(五招助你成为vuejs大师)
2018/05/04 Javascript
jQuery 点击获取验证码按钮及倒计时功能
2018/09/20 jQuery
Vue如何使用混合Mixins和插件开发详解
2020/02/05 Javascript
js实现微信聊天界面
2020/08/09 Javascript
JavaScript实现串行请求的示例代码
2020/09/14 Javascript
[01:22:19]EG vs TNC Supermajor小组赛B组败者组第一轮 BO3 第二场 6.2
2018/06/03 DOTA
Python写的一个简单DNS服务器实例
2014/06/04 Python
Python搭建HTTP服务器和FTP服务器
2017/03/09 Python
python中实现字符串翻转的方法
2018/07/11 Python
python实现从pdf文件中提取文本,并自动翻译的方法
2018/11/28 Python
Python和Java的语法对比分析语法简洁上python的确完美胜出
2019/05/10 Python
Python二次规划和线性规划使用实例
2019/12/09 Python
使用OpenCV-python3实现滑动条更新图像的Canny边缘检测功能
2019/12/12 Python
纯HTML5+CSS3制作图片旋转
2016/01/12 HTML / CSS
《九色鹿》教学反思
2014/02/27 职场文书
亮化工程实施方案
2014/03/17 职场文书
《郑和远航》教学反思
2014/04/16 职场文书
2014年商场工作总结
2014/11/22 职场文书
优秀教师推荐材料
2014/12/16 职场文书
2015年档案室工作总结
2015/05/23 职场文书
2019年手机市场的调研报告2篇
2019/10/10 职场文书
SQL中的三种去重方法小结
2021/11/01 SQL Server
云服务器部署 Web 项目的实现步骤
2022/06/28 Servers
Python软件包安装的三种常见方法
2022/07/07 Python