【Spring Resources 资源访问】

1 概述

Java的标准java.net.URL类和标准处理程序可以处理各种URL前缀的资源。不幸的是不能够充分的对底层资源的访问。例如,没有实现标准化的URL可以用来访问资源,从类路径中获取资源,从相对ServletContext中获取资源等等。

2 资源接口

public interface Resource extends InputStreamSource {

    /**
     * Return whether this resource actually exists in physical form.
     * <p>This method performs a definitive existence check, whereas the
     * existence of a {@code Resource} handle only guarantees a
     * valid descriptor handle.
     */
    boolean exists();

    /**
     * Return whether the contents of this resource can be read,
     * e.g. via {@link #getInputStream()} or {@link #getFile()}.
     * <p>Will be {@code true} for typical resource descriptors;
     * note that actual content reading may still fail when attempted.
     * However, a value of {@code false} is a definitive indication
     * that the resource content cannot be read.
     * @see #getInputStream()
     */
    boolean isReadable();

    /**
     * Return whether this resource represents a handle with an open
     * stream. If true, the InputStream cannot be read multiple times,
     * and must be read and closed to avoid resource leaks.
     * <p>Will be {@code false} for typical resource descriptors.
     */
    boolean isOpen();

    /**
     * Return a URL handle for this resource.
     * @throws IOException if the resource cannot be resolved as URL,
     * i.e. if the resource is not available as descriptor
     */
    URL getURL() throws IOException;

    /**
     * Return a URI handle for this resource.
     * @throws IOException if the resource cannot be resolved as URI,
     * i.e. if the resource is not available as descriptor
     */
    URI getURI() throws IOException;

    /**
     * Return a File handle for this resource.
     * @throws IOException if the resource cannot be resolved as absolute
     * file path, i.e. if the resource is not available in a file system
     */
    File getFile() throws IOException;

    /**
     * Determine the content length for this resource.
     * @throws IOException if the resource cannot be resolved
     * (in the file system or as some other known physical resource type)
     */
    long contentLength() throws IOException;

    /**
     * Determine the last-modified timestamp for this resource.
     * @throws IOException if the resource cannot be resolved
     * (in the file system or as some other known physical resource type)
     */
    long lastModified() throws IOException;

    /**
     * Create a resource relative to this resource.
     * @param relativePath the relative path (relative to this resource)
     * @return the resource handle for the relative resource
     * @throws IOException if the relative resource cannot be determined
     */
    Resource createRelative(String relativePath) throws IOException;

    /**
     * Determine a filename for this resource, i.e. typically the last
     * part of the path: for example, "myfile.txt".
     * <p>Returns {@code null} if this type of resource does not
     * have a filename.
     */
    String getFilename();

    /**
     * Return a description for this resource,
     * to be used for error output when working with the resource.
     * <p>Implementations are also encouraged to return this value
     * from their {@code toString} method.
     * @see Object#toString()
     */
    String getDescription();

}

3 资源实现类

Resource 接口的实现类有很多,这里着重介绍4个

3.1 UrlResource

UrlResource 是 java.net.URL的包装,可以用来读取各种网络资源,如http:协议、ftp:协议甚至本地文件系统file: 等等;

    Resource resource = null;
    // 读取网络资源
    resource = new UrlResource("http://www.baidu.com/index.html");

    // 读取本地文件系统资源
    resource = new UrlResource("file:/Users/Oracle/Desktop/ReadMe.txt");

3.2 ClassPathResource

ClassPathResource 用于读取类路径下的资源

    Resource resource = null;
    // 获取src/main/resource/jdbc.properties 属性文件
    resource = new ClassPathResource("jdbc.properties");

    // 带包的文件访问
    resource = new ClassPathResource("org/spring/resource/db.properties");

3.3 FileSystemResource

FileSystemResource 用于读取本地文件系统资源,必须制定绝对路径

    Resource resource = null;
    // 取绝对路径,否则找不到文件
    resource = new FileSystemResource("/Users/Oracle/Desktop/ReadMe.txt");

3.4 ServletContextResource

ServletContextResource 用于读取web根目录资源

    Resource resource = null;
    // tree.xml位于WEB-INF目录下,与web.xml同层
    resource = new ServletContextResource(application, "/WEB-INF/tree.xml");
posted @ 2016-10-04 15:11  Bruce.Chang.Lee  阅读(307)  评论(0)    收藏  举报