在Java中,将不同来源的资源抽象成URL,通过注册不同的handler(URLStreamHandler)来处理不同来源间的资源读取逻辑。而URL中却没有提供一些基本方法来实现自己的抽象结构。因而Spring提出了一套基于
org.springframework.core.io.Resource和org.springframework.core.io.ResourceLoader接口的资源抽象和加载策略。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 
 | package org.springframework.core.io;
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URL;
 
 
 * Resource接口抽象了所有Spring内部使用到的底层资源:File、URL、Classpath等。
 * 同时,对于来源不同的资源文件,Resource也有不同实现:文件(FileSystemResource)、Classpath资源(ClassPathResource)、
 * URL资源(UrlResource)、InputStream资源(InputStreamResource)、Byte数组(ByteArrayResource)等等。
 *
 * @author Juergen Hoeller
 * @since 28.12.2003
 */
 public interface Resource extends InputStreamSource {
 
 
 * 判断资源是否存在
 * @return boolean 是否存在
 */
 boolean exists();
 
 
 * 判断资源是否可读
 * @return boolean 是否可读
 */
 boolean isReadable();
 
 
 * 是否处于开启状态
 * @return boolean 是否开启
 */
 boolean isOpen();
 
 
 * 得到URL类型资源,用于资源转换
 * @return URL 得到URL类型
 * @throws IOException 如果资源不能打开则抛出异常
 */
 URL getURL() throws IOException;
 
 
 * 得到URI类型资源,用于资源转换
 * @return URI 得到URI类型
 * @throws IOException 如果资源不能打开则抛出异常
 */
 URI getURI() throws IOException;
 
 
 * 得到File类型资源,用于资源转换
 * @return File 得到File类型
 * @throws IOException 如果资源不能打开则抛出异常
 */
 File getFile() throws IOException;
 
 
 * 获取资源长度
 * @return long 资源长度
 * @throws IOException 如果资源不能打开则抛出异常
 */
 long contentLength() throws IOException;
 
 
 * 获取lastModified属性
 * @return long 获取lastModified
 * @throws IOException 如果资源不能打开则抛出异常
 */
 long lastModified() throws IOException;
 
 
 * 创建一个相对的资源方法
 * @param relativePath 相对路径
 * @return Resource 返回一个新的资源
 * @throws IOException 如果资源不能打开则抛出异常
 */
 Resource createRelative(String relativePath) throws IOException;
 
 
 * 获取文件名称
 * @return String 文件名称或者null
 */
 String getFilename();
 
 
 * 得到错误处理信息,主要用于错误处理的信息打印
 * @return String 错误资源信息
 */
 String getDescription();
 }
 
 |