<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>Spring中的org.springframework.core.io.Resource接口代表着物理存在的任何资源,其继承于org.springframework.core.io.InputStreamSource;其子类有如下几种:ByteArrayResource, ClassPathResource, DescriptiveResource, FileSystemResource, InputStreamResource, PortletContextResource, ServletContextResource, UrlResource 。常用的有以下四种:
- ClassPathResource:通过 ClassPathResource 以类路径的方式进行访问;
- FileSystemResource:通过 FileSystemResource 以文件系统绝对路径的方式进行访问;
- ServletContextResource:通过 ServletContextResource 以相对于Web应用根目录的方式进行访问。
- UrlResource :通过java.net.URL来访问资源,当然它也支持File格式,如“file:”
先看一下Resource接口的定义:
public
interface
Resource
extends
InputStreamSource
...
{
booleanexists();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
booleanisOpen();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
URLgetURL()throwsIOException;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
FilegetFile()throwsIOException;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ResourcecreateRelative(StringrelativePath)throwsIOException;//根据相对路径创建资源
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
StringgetFilename();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
StringgetDescription();
}
它提供我们访问资源最基本的接口。我们实际访问的时候直接用Resource接口就可以,不比使用其子类。在初始化时,其子类的实现各有不同。
首先看最常用的ClassPathResource :
public
InputStreamgetInputStream()
throws
IOException
...
{
InputStreamis=null;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(this.clazz!=null)...{
is=this.clazz.getResourceAsStream(this.path);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else...{
is=this.classLoader.getResourceAsStream(this.path);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(is==null)...{
thrownewFileNotFoundException(
getDescription()+"cannotbeopenedbecauseitdoesnotexist");
}
returnis;
}
这个是ClassPathResource 获得InputStream的代码,可以看出,它是通过Class或者ClassLoader的getResourceAsStream()方法来获得InputStream的。关于这两个类的方法的区别我另外的一篇文章已经解释。其path一般都是以“classpath:”开头,如果以“classpath*:”开头表示所有与给定名称匹配的classpath资源都应该被获取。
其次来看FileSystemResource:
public
FileSystemResource(Stringpath)
...
{
Assert.notNull(path,"Pathmustnotbenull");
this.file=newFile(path);
this.path=StringUtils.cleanPath(path);
}
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
InputStreamgetInputStream()
throws
IOException
...
{
returnnewFileInputStream(this.file);
}
就是根据给定的Path来构造File,然后再构造FileInputStream。这儿的path一般要给出绝对路径,当然也可以是相对路径,但是如果使用相对路径要注意其根目录。例如在eclipse中,它的根目录就是你工程目录作为你的根目录。
再来看ServletContextResource:
public
InputStreamgetInputStream()
throws
IOException
...
{
InputStreamis=this.servletContext.getResourceAsStream(this.path);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(is==null)...{
thrownewFileNotFoundException("Couldnotopen"+getDescription());
}
returnis;
}
ServletContextResource通过ServletContext的getResourceAsStream()来取得InputStream,这里path必须以“/”开头,并且相对于当前上下文的根目录。如常用path="/WEB-INF/web.xml"。
最后来看UrlResource:
public
UrlResource(Stringpath)
throws
MalformedURLException
...
{
Assert.notNull(path,"Pathmustnotbenull");
this.url=newURL(path);
this.cleanedUrl=getCleanedUrl(this.url,path);
}
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
InputStreamgetInputStream()
throws
IOException
...
{
URLConnectioncon=this.url.openConnection();
con.setUseCaches(false);
returncon.getInputStream();
}
UrlResource 封装了java.net.URL
,它能够被用来访问任何通过URL可以获得的对象,例如:文件、HTTP对象、FTP对象等。所有的URL都有个标准的 String
表示,这些标准前缀可以标识不同的URL类型,包括file:
访问文件系统路径,http:
通过HTTP协议访问的资源,ftp:
通过FTP访问的资源等等。
在Spring配置文件中,我们只需要给出字符串类型的path即可,Spring会通过ResourceEditor(java.beans.PropertyEditor的子类)和ResourceLoader把给定的path转换为相应的Resource。转换规则如下:
前缀 | 例子 | 说明 |
---|
classpath: | classpath:com/myapp/config.xml | 从classpath中加载。 |
file: | file:/data/config.xml | 作为 URL 从文件系统中加载。 |
http: | http://myserver/logo.png | 作为 URL 加载。 |
(none) | /data/config.xml | 根据 ApplicationContext 进行判断。 |
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1818714