[b]学习spring资源服务总结.[/b]
jdk所提供访问资源的类 java.net.url,file等;并不能很好的满足各种底层资源的访问需求,比如,没有类路径的资源文件,web容器的上下文中获取资源的操作类。为了解决这些问题。
spring 设计了一个Resource接口,提供更强的访问服务,此接口对应了不同的资源类型的实现类。先了解一下具体的资源实现类的结构图;
[img]http://dl.iteye.com/upload/picture/pic/78496/08ff1092-9034-3cb1-8fcd-6a60e13d5c4c.jpg[/img]
1 Resource 接口主要提供的方法:
1 ByteArrayResource:二进制数组表示的资源
2 ClassPathResource:类路径下的资源
3 FileSystemResource:文件系统资源
4 InputStreamResource:输入流返回表示资源
5 ServletContextResource:基于web容器上下文的资源
6 UrLResource:对java.io.URL的封装,可以表示很多的协议的网络资源(http,ftp,文件系统资源)
7 PortletContextResource:基于web容器上下文的资源
8 DescriptiveResource:
jdk所提供访问资源的类 java.net.url,file等;并不能很好的满足各种底层资源的访问需求,比如,没有类路径的资源文件,web容器的上下文中获取资源的操作类。为了解决这些问题。
spring 设计了一个Resource接口,提供更强的访问服务,此接口对应了不同的资源类型的实现类。先了解一下具体的资源实现类的结构图;
[img]http://dl.iteye.com/upload/picture/pic/78496/08ff1092-9034-3cb1-8fcd-6a60e13d5c4c.jpg[/img]
1 Resource 接口主要提供的方法:
public interface Resource extends InputStreamSource {
//资源是否存在。
boolean exists();
//是否可以读
boolean isReadable();
资//源是否打开
boolean isOpen();
//如果底层服力可以表示为urL,则返回url对象
URL getURL() throws IOException;
URI getURI() throws IOException;
//资源对应的一个文件
File getFile() throws IOException;
//最后修改时间
long lastModified() throws IOException;
//通过path 得到一个Resource对象
Resource createRelative(String relativePath) throws IOException;
//获取资源名称
String getFilename();
//资源描述
String getDescription();
}
1 ByteArrayResource:二进制数组表示的资源
2 ClassPathResource:类路径下的资源
3 FileSystemResource:文件系统资源
4 InputStreamResource:输入流返回表示资源
5 ServletContextResource:基于web容器上下文的资源
6 UrLResource:对java.io.URL的封装,可以表示很多的协议的网络资源(http,ftp,文件系统资源)
7 PortletContextResource:基于web容器上下文的资源
8 DescriptiveResource: