概述
Spring
对各种底层资源,比如文件系统中的一个文件,classpath上的一个文件,或者一个网络URL,统一抽象为接口Resource
来表示 。
org.springframework.core.io // 接口Resource所在包
因为每个底层文件都可以以一个只读InputStream
的方式打开,所以Resource
接口继承自接口InputStreamSource
。InputStreamSource
接口中定义了方法getInputStream()
用于返回打开该资源所对应的输入流。
实现类
Spring
针对不同的底层资源类型提供了相应的实现。如下表所示 :
实现类 | 介绍 |
---|---|
ClassPathResource | Java类路径上的一个资源文件, 比如一个class文件,xml/properties配置文件,亦或一个图片文件等等 |
FileSystemResource | 操作系统文件系统中的一个资源文件 一个xml/properties配置文件,一个图片,一个pdf,一个jar包等等 |
UrlResource | 将一个网络URL地址封装成一个资源文件 一个静态图片,html,js,css,或者能动态生成json/xml |
ByteArrayResource | 将一个字节数组封装成一个Resource |
ServletContextResource | 获取ServletContext 环境下的资源文件,在web应用程序的根目录下解释相对路径 |
InputStreamResource | 将一个输入流封装成一个资源,因为用于描述一个已经打开的资源,所以isOpen()总是返回true |
VfsResource | 针对JBoss VFS 的资源 |
使用实例
package test.resources;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.core.io.*;
import java.io.FileInputStream;
import java.io.InputStream;
@Slf4j
public class SpringResourceDemo {
/**
* 描述一个Resource实例
*
* @param resource
* @throws Exception
*/
void describe(Resource resource) throws Exception {
log.info("====================================");
log.info("toString : {}", resource.toString());
log.info("contentLength : {}", resource.contentLength());
log.info("exists : {}", resource.exists());
log.info("getDescription : {}", resource.getDescription());
log.info("isReadable : {}", resource.isReadable());
log.info("isOpen : {}", resource.isOpen());
log.info("getFilename : {}", resource.getFilename());
log.info("isFile : {}", resource.isFile());
if (resource.isFile()) {
// getFile()仅针对文件类型Resource有效,可以是文件系统文件或者classpath上的文件
log.info("getFile : {}", resource.getFile());
}
if (!((resource instanceof ByteArrayResource) || (resource instanceof InputStreamResource))) {
// 以下三个属性针对 ByteArrayResource/InputStreamResource 类型资源无效,调用的话会抛出异常
log.info("lastModified : {}", resource.lastModified());
log.info("getURI : {}", resource.getURI());
log.info("getURL : {}", resource.getURL());
}
}
@Test
public void test() throws Exception {
{
Resource resource = new ClassPathResource("test/resources/SpringResourceDemo.class");
describe(resource);
}
{// test.txt 是当前磁盘卷根目录下一个存在的文件,内容是:Test Spring Resource
Resource resource = new FileSystemResource("/test.txt");
describe(resource);
}
{//
Resource resource = new UrlResource("https://docs.spring.io");
describe(resource);
}
{//
Resource resource = new ByteArrayResource("Hello".getBytes());
describe(resource);
}
{// test.txt 是当前磁盘卷根目录下一个存在的文件,内容是:Test Spring Resource
InputStream is = new FileInputStream("/test.txt");
Resource resource = new InputStreamResource(is);
describe(resource);
}
}
}
上面的测试代码执行输出如下:
====================================
toString : class path resource [test/resources/SpringResourceDemo.class]
contentLength : 2838
exists : true
getDescription : class path resource [test/resources/SpringResourceDemo.class]
isReadable : true
isOpen : false
getFilename : SpringResourceDemo.class
isFile : true
getFile : D:\springboot-tut-zero\target\test-classes\test\resources\SpringResourceDemo.class
lastModified : 1542634668385
getURI : file:/D:/springboot-tut-zero/target/test-classes/test/resources/SpringResourceDemo.class
getURL : file:/D:/springboot-tut-zero/target/test-classes/test/resources/SpringResourceDemo.class
====================================
toString : file [D:\test.txt]
contentLength : 20
exists : true
getDescription : file [D:\test.txt]
isReadable : true
isOpen : false
getFilename : test.txt
isFile : true
getFile : \test.txt
lastModified : 1542633020067
getURI : file:/D:/test.txt
getURL : file:/D:/test.txt
====================================
toString : URL [https://docs.spring.io]
contentLength : -1
exists : true
getDescription : URL [https://docs.spring.io]
isReadable : true
isOpen : false
getFilename :
isFile : false
lastModified : 0
getURI : https://docs.spring.io
getURL : https://docs.spring.io
====================================
toString : Byte array resource [resource loaded from byte array]
contentLength : 5
exists : true
getDescription : Byte array resource [resource loaded from byte array]
isReadable : true
isOpen : false
getFilename : null
isFile : false
====================================
toString : InputStream resource [resource loaded through InputStream]
contentLength : 20
exists : true
getDescription : InputStream resource [resource loaded through InputStream]
isReadable : true
isOpen : true
getFilename : null
isFile : false