1、可以看到有四個比較重要的接口 InputStreamSource、Resource、WritableResource、ContextResource。 a 、InputStreamSource接口
public interface InputStreamSource {/*** Return an {@link InputStream}.* <p>It is expected that each call creates a <i>fresh</i> stream.* <p>This requirement is particularly important when you consider an API such* as JavaMail, which needs to be able to read the stream multiple times when* creating mail attachments. For such a use case, it is <i>required</i>* that each {@code getInputStream()} call returns a fresh stream.* @return the input stream for the underlying resource (must not be {@code null})* @throws IOException if the stream could not be opened* @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource)*/InputStream getInputStream() throws IOException;
}
public interface Resource extends InputStreamSource {/*** 判斷是否存在*/boolean exists();/*** 判斷是否可讀*/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.* 判斷流是否可以重復讀取,如果為true的話表示不可以重復讀取,在讀取完成后需要關閉流*/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();}
public interface ContextResource extends Resource {/*** Return the path within the enclosing 'context'.* <p>This is typically path relative to a context-specific root directory,* e.g. a ServletContext root or a PortletContext root.*/String getPathWithinContext();}
public interface ResourceLoader {/** Pseudo URL prefix for loading from the class path: "classpath:" */String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;/*** 用來根據location來獲取對應的資源*/Resource getResource(String location);/*** 獲取類加載器*/ClassLoader getClassLoader();}
public Resource getResource(String location) {Assert.notNull(location, "Location must not be null");// 以/開頭,那么根據path去查找if (location.startsWith("/")) {return getResourceByPath(location);}// 以classpath開頭,那么抽象為ClassPathResourceelse if (location.startsWith(CLASSPATH_URL_PREFIX)) {return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());}else {try {// 其他情況采用UrlResource來進行加載URL url = new URL(location);return new UrlResource(url);}catch (MalformedURLException ex) {// No URL -> resolve as resource path.return getResourceByPath(location);}}}
public class EncodedResource {private final Resource resource;private final String encoding;private final Charset charset;/*** 根據encoding和charset是否存在來判斷是否可以獲取Reader*/public boolean requiresReader() {return (this.encoding != null || this.charset != null);}/*** 根據EncodedResource信息獲取Reader信息*/public Reader getReader() throws IOException {if (this.charset != null) {return new InputStreamReader(this.resource.getInputStream(), this.charset);}else if (this.encoding != null) {return new InputStreamReader(this.resource.getInputStream(), this.encoding);}else {return new InputStreamReader(this.resource.getInputStream());}}/*** Open a {@code java.io.InputStream} for the specified resource, ignoring any* specified {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}.* @throws IOException if opening the InputStream failed* @see #requiresReader()* @see #getReader()*/public InputStream getInputStream() throws IOException {return this.resource.getInputStream();}}
2、ResourcePatternUtils
/*** Return whether the given resource location is a URL: either a* special "classpath" or "classpath*" pseudo URL or a standard URL*/public static boolean isUrl(String resourceLocation) {return (resourceLocation != null &&(resourceLocation.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX) ||ResourceUtils.isUrl(resourceLocation)));}/*** 根據ResourceLoader構建一個ResourcePatternResolver*/public static ResourcePatternResolver getResourcePatternResolver(ResourceLoader resourceLoader) {Assert.notNull(resourceLoader, "ResourceLoader must not be null");if (resourceLoader instanceof ResourcePatternResolver) {return (ResourcePatternResolver) resourceLoader;}else if (resourceLoader != null) {return new PathMatchingResourcePatternResolver(resourceLoader);}else {return new PathMatchingResourcePatternResolver();}}