- Spring开发框架专门提供了一组资源的处理接口:
org.springframework.core.io.Resource
(org.springframework.core.io.InputStreamSource子接口)
Resource接口主要是进行资源的数据读取
- 内存读取(ByteArrayResource)
- CLASSPATH读取(ClassPathResource)
- 文件读取(FileSystemResource)
- URL读取(UrlResource)
- WEB上下文读取(ServletContextResource)
Resource接口提供了如下常用处理方法:
- public boolean exists() --该资源是否存在
- public default boolean isReadable() --该资源是否可读
- public long contentLength() --获取内容长度
- public long lastModified() --取得最后一次修改日期时间
- public String getFilename() --取得文件名
- public InputStream getInputStream() --取得输入流对象(父类org.springframework.core.io.InputStreamSource方法)
测试代码:
package cn.liang.demo;
import java.io.File;
import java.util.Scanner;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
public class ResourceDemo01 {
public static void main(String[] args) throws Exception {
//内存操作流
String info = "hello world liang" ;
Resource resourceByte = new ByteArrayResource(info.getBytes()) ;
print(resourceByte);
//文件读取
File file = new File("/etc/hosts");
Resource resourceFile = new FileSystemResource(file) ;
print(resourceFile);
//基于CLASSPATH的数据读取
String classpath = "applicationContext.xml";
Resource resourceClassPath = new ClassPathResource(classpath) ;
print(resourceClassPath);
//读取URL
String url = "http://www.baidu.com";
Resource resourceUrl = new UrlResource(url) ;
print(resourceUrl);
}
public static void print(Resource resource) throws Exception{
if (resource.exists()) { // 表示该资源存在
// Resource的父接口InputStreamResource只提供有getInputStream()方法,所以这个时候最好的读取使用Scanner
Scanner scan = new Scanner(resource.getInputStream()) ;
scan.useDelimiter(" ") ;
while (scan.hasNext()) {
System.out.println(scan.nextLine());
}
scan.close();
}
System.out.println("*********************");
}
}
ResourceLoader接口
- 简化Resource读取资源以及操作子类不通区分的需求
- 接口路径:org.springframework.core.io.ResourceLoader
- 可以使用org.springframework.core.io.DefaultResourceLoader子类实例化
- ResourceLoader接口的getResource()方法可以接收一个表示资源路径的字符串数据
- 读取文件系统资源:file:路径
- 读取网络资源:http://路径
- CLASSPATH读取:classpath:路径
测试程序:
package cn.liang.demo;
import java.util.Scanner;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public class ResourceDemo02 {
public static void main(String[] args) throws Exception {
//读取文件资源
String filePath = "/etc/hosts";
ResourceLoader loaderFile = new DefaultResourceLoader() ;
Resource resourceFile = loaderFile.getResource("file:" + filePath) ;
print(resourceFile);
//读取网络资源
String Url = "http://www.baidu.com";
ResourceLoader loaderUrl = new DefaultResourceLoader() ;
Resource resourceUrl = loaderUrl.getResource(Url) ;
print(resourceUrl);
//读取CLASSPATH资源
String classpath = "classpath:applicationContext.xml";
ResourceLoader loaderClassPath = new DefaultResourceLoader() ;
Resource resourceClassPath = loaderClassPath.getResource(classpath) ;
print(resourceClassPath);
}
public static void print(Resource resource) throws Exception{
if (resource.exists()) {
Scanner scan = new Scanner(resource.getInputStream()) ;
scan.useDelimiter(" ") ;
while (scan.hasNext()) {
System.out.println(scan.nextLine());
}
scan.close();
}
System.out.println("*********************");
}
}