您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

springboot如何读取resources下的文件

2024/2/22 23:55:05发布23次查看
项目中很多时候需要读取自定义配置文件,本地开发工具怎么写都成功但是部署到服务其上就出现问题,
异常boot-inf/classes!/config.xml (文件名、目录名或卷标语法不正确.)路径中带有叹号之类的
了解了大概之后就是springboot打成jar是一个文件,也就是一个压缩包,没有办法读取压缩文件里的路径,因此要解决这个问题了解读取配置文件的原理,直接获取文件流就可以了。
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)@springboottestpublic 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下的文件的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product