
1、测试resources下的文件
@Test
public void test() throws Exception {
/**
*
* classpath: 例如classpath:com/cad/domain/bean.xml。从类路径中加载资源
*
* file:例如 file:com/cad/domain/bean.xml.使用UrlResource从文件系统目录中加载资源。
*
* http:// 例如http://www.baidu.com/resource/bean.xml 使用UrlResource从web服务器加载资源
*
* ftp:// 例如frp://10.22.10.11/bean.xml 使用UrlResource从ftp服务器加载资源
* */
ResourceLoader resolver = new PathMatchingResourcePatternResolver();
Resource resource1 = resolver.getResource("classpath:db.properties");
InputStream inputStream = resource1.getInputStream();
byte[] b=new byte[1024];
int len=0;
//输出文件内容
while ((len = inputStream.read(b))!=-1){
System.out.println(new String(b));
}
inputStream.close();
}

2、测试java下的文件
@Test
public void test() throws Exception {
/**
*
* classpath: 例如classpath:com/cad/domain/bean.xml。从类路径中加载资源
*
* file:例如 file:com/cad/domain/bean.xml.使用UrlResource从文件系统目录中加载资源。
*
* http:// 例如http://www.baidu.com/resource/bean.xml 使用UrlResource从web服务器加载资源
*
* ftp:// 例如frp://10.22.10.11/bean.xml 使用UrlResource从ftp服务器加载资源
* */
ResourceLoader resolver = new PathMatchingResourcePatternResolver();
Resource resource1 = resolver.getResource("classpath:com\\lihua\\test.txt");
InputStream inputStream = resource1.getInputStream();
byte[] b=new byte[1024];
int len=0;
//输出文件内容
while ((len = inputStream.read(b))!=-1){
System.out.println(new String(b));
}
inputStream.close();
}

3、验证

删除 db.properties 后 再次运行

由此说明:classpath:指定的目录是classes
本文介绍了如何在Java中使用ResourceLoader加载classpath、file、HTTP和FTP等不同类型的资源,并通过实例展示了classpath指定的资源目录为classes。重点在于Resource的路径配置和实际操作。
321

被折叠的 条评论
为什么被折叠?



