笔记:Spring中获取classpath下的文件资源

来源:互联网 发布:java架构师是做什么的 编辑:程序博客网 时间:2024/06/05 04:27

通过Spring工具类获取classpath下的文件资源

1、资源文件不在jar

 

File cfgFile = ResourceUtils.getFile("classpath:test.txt");或者org.springframework.core.io.Resource fileRource = new ClassPathResource("test.txt");

 

获取文件:fileRource.getFile();

获取文件流:fileRource.getInputStream();

2、资源文件在jar

 

org.springframework.core.io.Resource fileRource = new ClassPathResource("test.txt");

 

获取文件流:fileRource.getInputStream();

注:不管是文件系统中的类资源,还是JAR中的类资源文件,都可以以流的形式读取

参考:

陈雄华:《使用如下方式读取JAR中的文件出错》http://stamen.iteye.com/blog/1485531

http://blog.csdn.net/liu251/article/details/2899023

如何读取JAR包中的资源文件(如XMLTXT等) :http://blog.csdn.net/liu251/article/details/4057140

阅读全文
0 0
原创粉丝点击