今天来分析Spring的资源接口Resource的各个实现类。关于它的接口和抽象类,参见上一篇博文——Spring源码分析——资源访问利器Resource之接口和抽象类分析
一、文件系统资源 FileSystemResource
文件系统资源 FileSystemResource,资源以文件系统路径的方式表示。这个类继承自AbstractResource,并实现了写的接口WritableResource。类全称为public class FileSystemResource extends AbstractResource implements WritableResource 。这个资源类是所有Resource实现类中,唯一一个实现了WritableResource接口的类。就是说,其他的类都不可写入操作,都只能读取。部分翻译注释后,源码如下:(以后不重要的源码我就折叠起来)
/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.core.io;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URI;import java.net.URL;import org.springframework.util.Assert;import org.springframework.util.StringUtils;/** * @author Juergen Hoeller * @since 28.12.2003 * @see java.io.File */public class FileSystemResource extends AbstractResource implements WritableResource { private final File file; // 不可变属性 private final String path; // 不可变属性 public FileSystemResource(File file) { // 简单的构造方法,path为file路径格式化后的样子 Assert.notNull(file, "File must not be null"); this.file = file; this.path = StringUtils.cleanPath(file.getPath()); } public FileSystemResource(String path) { //简单的构造方法 Assert.notNull(path, "Path must not be null"); this.file = new File(path); this.path = StringUtils.cleanPath(path); } public final String getPath() { //新增的方法,返回资源路径,方法不可重写 return this.path; } @Override public boolean exists() { return this.file.exists(); } @Override public boolean isReadable() { return (this.file.canRead() && !this.file.isDirectory()); } public InputStream getInputStream() throws IOException { //InputStreamSource接口的实现方法 return new FileInputStream(this.file); } @Override public URL getURL() throws IOException { //可见这个url是通过uri得到的 return this.file.toURI().toURL(); } @Override public URI getURI() throws IOException { return this.file.toURI(); } @Override public File getFile() { return this.file; } @Override public long contentLength() throws IOException { return this.file.length(); } @Override public Resource createRelative(String relativePath) { String pathToUse = StringUtils.applyRelativePath(this.path, relativePath); return new FileSystemResource(pathToUse); } @Override public String getFilename() { return this.file.getName(); } public String getDescription() { // 资源描述,直接用绝对路径来构造 return "file [" + this.file.getAbsolutePath() + "]"; } public boolean isWritable() { // WritableResource接口的实现方法 return (this.file.canWrite() && !this.file.isDirectory()); } public OutputStream getOutputStream() throws IOException { return new FileOutputStream(this.file); } @Override public boolean equals(Object obj) { //通过path来比较 return (obj == this || (obj instanceof FileSystemResource && this.path.equals(((FileSystemResource) obj).path))); } @Override public int hashCode() { // 文件资源的HashCode就是path的hashCode return this.path.hashCode(); }}View Code
结论:
1、这个类由2个不可变的属性 file 和 path ,本质上就是一个java.io.File 的包装。
2、值得一提的是,与父类AbstractResource不同的是,这个类的 equals() 和 hashcode() 都通过属性 path 来操作。
测试:
public class FileSytemResourceTest { public static void main(String[] args) { String path = "E:/java/abc.txt"; Resource resource = new FileSystemResource(path); System.out.println("resource1 : "+resource.getFilename()); File f = new File("text.txt"); Resource resource2 = new FileSystemResource(f); System.out.println("resource2 : "+resource2.getFilename()); }}
结果:
resource1 : abc.txtresource2 : text.txt
二、字节数组资源——ByteArrayResource
字节数组资源ByteArrayResource,资源即,字节数组。
这个类很简单,也没必要翻译,仅仅是一个不可变的字节数组加一个不可变的描述字符串的包装,源码如下:
/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.core.io;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Arrays;/** * {@link Resource} implementation for a given byte array. * Creates a ByteArrayInputStreams for the given byte array. * * <p>Useful for loading content from any given byte array, * without having to resort to a single-use {@link InputStreamResource}. * Particularly useful for creating mail attachments from local content, * where JavaMail needs to be able to read the stream multiple times. * * @author Juergen Hoeller * @since 1.2.3 * @see java.io.ByteArrayInputStream * @see InputStreamResource * @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource) */public class ByteArrayResource extends AbstractResource { private final byte[] byteArray; private final String description; /** * Create a new ByteArrayResource. * @param byteArray the byte array to wrap */ public ByteArrayResource(byte[] byteArray) { this(byteArray, "resource loaded from byte array"); } /** * Create a new ByteArrayResource. * @param byteArray the byte array to wrap * @param description where the byte array comes from */ public ByteArrayResource(byte[] byteArray, String description) { if (byteArray == null) { throw new IllegalArgumentException("Byte array must not be null"); } this.byteArray = byteArray; this.description = (description != null ? description : ""); } /** * Return the underlying byte array. */ public final byte[] getByteArray() { return this.byteArray; } /** * This implementation always returns {@code true}. */ @Override public boolean exists() { return true; } /** * This implementation returns the length of the underlying byte array. */ @Override public long contentLength() { return this.byteArray.length; } /** * This implementation returns a ByteArrayInputStream for the * underlying byte array. * @see java.io.ByteArrayInputStream */ public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(this.byteArray); } /** * This implementation returns the passed-in description, if any. */ public String getDescription() { return this.description; } /** * This implementation compares the underlying byte array. * @see java.util.Arrays#equals(byte[], byte[]) */ @Override public boolean equals(Object obj) { return (obj == this || (obj instanceof ByteArrayResource && Arrays.equals(((ByteArrayResource) obj).byteArray, this.byteArray))); } /** * This implementation returns the hash code based on the * underlying byte array. */ @Override public int hashCode() { return (byte[].class.hashCode() * 29 * this.byteArray.length); }}View Code
若需要操作描述一个字节数组,可以用这个资源类。ByteArrayResource可多次读取数组资源。
三、描述性资源——DescriptiveResource 描述性资源DescriptiveResource,这个类更简单,仅仅一个不可变的描述字符串的包装,源码如下:
/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.core.io;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;/** * Simple {@link Resource} implementation that holds a resource description * but does not point to an actually readable resource. * * <p>To be used as placeholder if a {@code Resource} argument is * expected by an API but not necessarily used for actual reading. * * @author Juergen Hoeller * @since 1.2.6 */public class DescriptiveResource extends AbstractResource { private final String description; /** * Create a new DescriptiveResource. * @param description the resource description */ public DescriptiveResource(String description) { this.description = (description != null ? description : ""); } @Override public boolean exists() { return false; } @Override public boolean isReadable() { return false; } public InputStream getInputStream() throws IOException { throw new FileNotFoundException( getDescription() + " cannot be opened because it does not point to a readable resource"); } public String getDescription() { return this.description; } /** * This implementation compares the underlying description String. */ @Override public boolean equals(Object obj) { return (obj == this || (obj instanceof DescriptiveResource && ((DescriptiveResource) obj).description.equals(this.description))); } /** * This implementation returns the hash code of the underlying description String. */ @Override public int hashCode() { return this.description.hashCode(); }}View Code
若一个资源,仅仅有一个描述,非常抽象的这种情况,可以用这个资源类,它并没有指向一个实际的可读的资源。一般用的非常稀少。个人觉得用处不大。 四、输入流资源——InputStreamResource 输入流资源InputStreamResource,是一个不可变InputStream的包装和一个不可变的描述字符串。此外还有一个私有成员变量Boolean read用于限制本资源的InputStream不可被重复获取。View Code
/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.core.io;import java.io.IOException;import java.io.InputStream;/** * {@link Resource} implementation for a given InputStream. Should only * be used if no specific Resource implementation is applicable. * In particular, prefer {@link ByteArrayResource} or any of the * file-based Resource implementations where possible. * * <p>In contrast to other Resource implementations, this is a descriptor * for an <i>already opened</i> resource - therefore returning "true" from * {@code isOpen()}. Do not use it if you need to keep the resource * descriptor somewhere, or if you need to read a stream multiple times. * * @author Juergen Hoeller * @since 28.12.2003 * @see ByteArrayResource * @see ClassPathResource * @see FileSystemResource * @see UrlResource */public class InputStreamResource extends AbstractResource { private final InputStream inputStream; private final String description; private boolean read = false; /** * Create a new InputStreamResource. * @param inputStream the InputStream to use */ public InputStreamResource(InputStream inputStream) { this(inputStream, "resource loaded through InputStream"); } /** * Create a new InputStreamResource. * @param inputStream the InputStream to use * @param description where the InputStream comes from */ public InputStreamResource(InputStream inputStream, String description) { if (inputStream == null) { throw new IllegalArgumentException("InputStream must not be null"); } this.inputStream = inputStream; this.description = (description != null ? description : ""); } /** * This implementation always returns {@code true}. */ @Override public boolean exists() { return true; } /** * This implementation always returns {@code true}. */ @Override public boolean isOpen() { return true; } /** * This implementation throws IllegalStateException if attempting to * read the underlying stream multiple times. */ public InputStream getInputStream() throws IOException, IllegalStateException { if (this.read) { throw new IllegalStateException("InputStream has already been read - " + "do not use InputStreamResource if a stream needs to be read multiple times"); } this.read = true; return this.inputStream; } /** * This implementation returns the passed-in description, if any. */ public String getDescription() { return this.description; } /** * This implementation compares the underlying InputStream. */ @Override public boolean equals(Object obj) { return (obj == this || (obj instanceof InputStreamResource && ((InputStreamResource) obj).inputStream.equals(this.inputStream))); } /** * This implementation returns the hash code of the underlying InputStream. */ @Override public int hashCode() { return this.inputStream.hashCode(); }}View Code
简单而言,这是一个InputStream的包装类,这个包装类指向的是一个已经打开的资源,所以它的 isOpen()总是返回true。而且它不能重复获取资源,只能读取一次。关闭资源也只能通过其中的InputStream来关闭。个人认为,用处有限。五、VFS资源——VfsResource vfs是Virtual File System虚拟文件系统,也称为虚拟文件系统开关(Virtual Filesystem Switch).是linux档案系统对外的接口。任何要使用档案系统的程序都必须经由这层接口来使用它。(摘自百度百科...)它能一致的访问物理文件系统、jar资源、zip资源、war资源等,VFS能把这些资源一致的映射到一个目录上,访问它们就像访问物理文件资源一样,而其实这些资源不存在于物理文件系统。 这个资源类包装类一个Object对象,所有的操作都是通过这个包装的对象的反射来实现的。这里就没必要贴源码了。 可以参考下面的用法:
@Test public void testVfsResourceForRealFileSystem() throws IOException { //1.创建一个虚拟的文件目录 VirtualFile home = VFS.getChild("/home"); //2.将虚拟目录映射到物理的目录 VFS.mount(home, new RealFileSystem(new File("d:"))); //3.通过虚拟目录获取文件资源 VirtualFile testFile = home.getChild("test.txt"); //4.通过一致的接口访问 Resource resource = new VfsResource(testFile); if(resource.exists()) { dumpStream(resource); } System.out.println("path:" + resource.getFile().getAbsolutePath()); Assert.assertEquals(false, resource.isOpen()); } @Test public void testVfsResourceForJar() throws IOException { //1.首先获取jar包路径 File realFile = new File("lib/org.springframework.beans-3.0.5.RELEASE.jar"); //2.创建一个虚拟的文件目录 VirtualFile home = VFS.getChild("/home2"); //3.将虚拟目录映射到物理的目录 VFS.mountZipExpanded(realFile, home, TempFileProvider.create("tmp", Executors.newScheduledThreadPool(1))); //4.通过虚拟目录获取文件资源 VirtualFile testFile = home.getChild("META-INF/spring.handlers"); Resource resource = new VfsResource(testFile); if(resource.exists()) { dumpStream(resource); } System.out.println("path:" + resource.getFile().getAbsolutePath()); Assert.assertEquals(false, resource.isOpen()); }View Code
六、Portlet上下文资源——PortletContextResource
Portlet是基于java的web组件,由portlet容器管理,并由容器处理请求,生产动态内容。这个资源类封装了一个不可变的javax.portlet.PortletContext对象和一个不可变的String对象代表路径。类中所有操作都基于这两个属性。PortletContextResource对象实现了ContextResource接口,实现了方法String getPathWithinContext(),即返回自身的path属性。
源码如下:
/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.web.portlet.context;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import javax.portlet.PortletContext;import org.springframework.core.io.AbstractFileResolvingResource;import org.springframework.core.io.ContextResource;import org.springframework.core.io.Resource;import org.springframework.util.Assert;import org.springframework.util.ResourceUtils;import org.springframework.util.StringUtils;import org.springframework.web.portlet.util.PortletUtils;/** * {@link org.springframework.core.io.Resource} implementation for * {@link javax.portlet.PortletContext} resources, interpreting * relative paths within the portlet application root directory. * * <p>Always supports stream access and URL access, but only allows * {@code java.io.File} access when the portlet application archive * is expanded. * * @author Juergen Hoeller * @author John A. Lewis * @since 2.0 * @see javax.portlet.PortletContext#getResourceAsStream * @see javax.portlet.PortletContext#getRealPath */public class PortletContextResource extends AbstractFileResolvingResource implements ContextResource { private final PortletContext portletContext; private final String path; /** * Create a new PortletContextResource. * <p>The Portlet spec requires that resource paths start with a slash, * even if many containers accept paths without leading slash too. * Consequently, the given path will be prepended with a slash if it * doesn't already start with one. * @param portletContext the PortletContext to load from * @param path the path of the resource */ public PortletContextResource(PortletContext portletContext, String path) { // check PortletContext Assert.notNull(portletContext, "Cannot resolve PortletContextResource without PortletContext"); this.portletContext = portletContext; // check path Assert.notNull(path, "Path is required"); String pathToUse = StringUtils.cleanPath(path); if (!pathToUse.startsWith("/")) { pathToUse = "/" + pathToUse; } this.path = pathToUse; } /** * Return the PortletContext for this resource. */ public final PortletContext getPortletContext() { return this.portletContext; } /** * Return the path for this resource. */ public final String getPath() { return this.path; } /** * This implementation checks {@code PortletContext.getResource}. * @see javax.portlet.PortletContext#getResource(String) */ @Override public boolean exists() { try { URL url = this.portletContext.getResource(this.path); return (url != null); } catch (MalformedURLException ex) { return false; } } /** * This implementation delegates to {@code PortletContext.getResourceAsStream}, * which returns {@code null} in case of a non-readable resource (e.g. a directory). * @see javax.portlet.PortletContext#getResourceAsStream(String) */ @Override public boolean isReadable() { InputStream is = this.portletContext.getResourceAsStream(this.path); if (is != null) { try { is.close(); } catch (IOException ex) { // ignore } return true; } else { return false; } } /** * This implementation delegates to {@code PortletContext.getResourceAsStream}, * but throws a FileNotFoundException if not found. * @see javax.portlet.PortletContext#getResourceAsStream(String) */ public InputStream getInputStream() throws IOException { InputStream is = this.portletContext.getResourceAsStream(this.path); if (is == null) { throw new FileNotFoundException("Could not open " + getDescription()); } return is; } /** * This implementation delegates to {@code PortletContext.getResource}, * but throws a FileNotFoundException if no resource found. * @see javax.portlet.PortletContext#getResource(String) */ @Override public URL getURL() throws IOException { URL url = this.portletContext.getResource(this.path); if (url == null) { throw new FileNotFoundException( getDescription() + " cannot be resolved to URL because it does not exist"); } return url; } /** * This implementation resolves "file:" URLs or alternatively delegates to * {@code PortletContext.getRealPath}, throwing a FileNotFoundException * if not found or not resolvable. * @see javax.portlet.PortletContext#getResource(String) * @see javax.portlet.PortletContext#getRealPath(String) */ @Override public File getFile() throws IOException { URL url = getURL(); if (ResourceUtils.isFileURL(url)) { // Proceed with file system resolution... return super.getFile(); } else { String realPath = PortletUtils.getRealPath(this.portletContext, this.path); return new File(realPath); } } @Override public Resource createRelative(String relativePath) { String pathToUse = StringUtils.applyRelativePath(this.path, relativePath); return new PortletContextResource(this.portletContext, pathToUse); } @Override public String getFilename() { return StringUtils.getFilename(this.path); } public String getDescription() { return "PortletContext resource [" + this.path + "]"; } public String getPathWithinContext() { return this.path; } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof PortletContextResource) { PortletContextResource otherRes = (PortletContextResource) obj; return (this.portletContext.equals(otherRes.portletContext) && this.path.equals(otherRes.path)); } return false; } @Override public int hashCode() { return this.path.hashCode(); }}View Code
这个类非常简单,并没有什么需要注意的。
七、Servlet上下文资源——ServletContextResource
Servlet这个大家都知道。这个资源类是为了访问Web容器上下文的资源而封装的类,可以以相对于Web应用根目录的路径加载资源。这个资源类封装了一个不可变的javax.servlet.ServletContext对象和一个不可变的String对象代表路径。类中所有操作都基于这两个属性。PortletContextResource对象实现了ContextResource接口,实现了方法String getPathWithinContext(),即返回自身的path属性。
这个类的实现基本就是基于 this.servletContext.getResource(this.path) 或 this.servletContext.getResourceAsStream(this.path) 这两个方法。
典型的,例如这个方法:
public InputStream getInputStream() throws IOException { InputStream is = this.servletContext.getResourceAsStream(this.path); if (is == null) { throw new FileNotFoundException("Could not open " + getDescription()); } return is; }
又如这个方法:
public URL getURL() throws IOException { URL url = this.servletContext.getResource(this.path); if (url == null) { throw new FileNotFoundException( getDescription() + " cannot be resolved to URL because it does not exist"); } return url; }
贴一下源码:
/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.web.context.support;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import javax.servlet.ServletContext;import org.springframework.core.io.AbstractFileResolvingResource;import org.springframework.core.io.ContextResource;import org.springframework.core.io.Resource;import org.springframework.util.Assert;import org.springframework.util.ResourceUtils;import org.springframework.util.StringUtils;import org.springframework.web.util.WebUtils;/** * {@link org.springframework.core.io.Resource} implementation for * {@link javax.servlet.ServletContext} resources, interpreting * relative paths within the web application root directory. * * <p>Always supports stream access and URL access, but only allows * {@code java.io.File} access when the web application archive * is expanded. * * @author Juergen Hoeller * @since 28.12.2003 * @see javax.servlet.ServletContext#getResourceAsStream * @see javax.servlet.ServletContext#getResource * @see javax.servlet.ServletContext#getRealPath */public class ServletContextResource extends AbstractFileResolvingResource implements ContextResource { private final ServletContext servletContext; private final String path; /** * Create a new ServletContextResource. * <p>The Servlet spec requires that resource paths start with a slash, * even if many containers accept paths without leading slash too. * Consequently, the given path will be prepended with a slash if it * doesn't already start with one. * @param servletContext the ServletContext to load from * @param path the path of the resource */ public ServletContextResource(ServletContext servletContext, String path) { // check ServletContext Assert.notNull(servletContext, "Cannot resolve ServletContextResource without ServletContext"); this.servletContext = servletContext; // check path Assert.notNull(path, "Path is required"); String pathToUse = StringUtils.cleanPath(path); if (!pathToUse.startsWith("/")) { pathToUse = "/" + pathToUse; } this.path = pathToUse; } /** * Return the ServletContext for this resource. */ public final ServletContext getServletContext() { return this.servletContext; } /** * Return the path for this resource. */ public final String getPath() { return this.path; } /** * This implementation checks {@code ServletContext.getResource}. * @see javax.servlet.ServletContext#getResource(String) */ @Override public boolean exists() { try { URL url = this.servletContext.getResource(this.path); return (url != null); } catch (MalformedURLException ex) { return false; } } /** * This implementation delegates to {@code ServletContext.getResourceAsStream}, * which returns {@code null} in case of a non-readable resource (e.g. a directory). * @see javax.servlet.ServletContext#getResourceAsStream(String) */ @Override public boolean isReadable() { InputStream is = this.servletContext.getResourceAsStream(this.path); if (is != null) { try { is.close(); } catch (IOException ex) { // ignore } return true; } else { return false; } } /** * This implementation delegates to {@code ServletContext.getResourceAsStream}, * but throws a FileNotFoundException if no resource found. * @see javax.servlet.ServletContext#getResourceAsStream(String) */ public InputStream getInputStream() throws IOException { InputStream is = this.servletContext.getResourceAsStream(this.path); if (is == null) { throw new FileNotFoundException("Could not open " + getDescription()); } return is; } /** * This implementation delegates to {@code ServletContext.getResource}, * but throws a FileNotFoundException if no resource found. * @see javax.servlet.ServletContext#getResource(String) */ @Override public URL getURL() throws IOException { URL url = this.servletContext.getResource(this.path); if (url == null) { throw new FileNotFoundException( getDescription() + " cannot be resolved to URL because it does not exist"); } return url; } /** * This implementation resolves "file:" URLs or alternatively delegate
新闻热点
疑难解答