您的位置:首页 > 编程语言 > Java开发

关于Spring工具类获取classpath下的文件资源

2015-01-22 20:49 239 查看
今天用到spring的工具类去获取classpath下的资源文件,在此记录下

1、资源文件不在jar中

import java.io.File;
import org.springframework.util.ResourceUtils;
File testFile = ResourceUtils.getFile("classpath:test.txt");


或者

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


获取文件:fileRource.getFile();

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

2、资源文件在jar中

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Resource fileRource = new ClassPathResource("test.txt");
获取文件流:fileRource.getInputStream();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐