Spring Resources概述
Java的标准iava.net.URL类和各种URL前缀的标准处理程序无法满足所有对low-evel资源的访问,比如: 没有标准化的URL实现可用于访问需要从类路径或相对于 ServletContext 获取的资源。并且缺少某些Spring所需要的功能,例如检测某资源是否存在等。而Spring的Resource声明了访问low-level资源的能力。
Resource接口
Spring的 Resource 接口位于org.springframework.core.io 中。旨在成为一个更强大的接口,用于抽象对低级资源的访问。以下显示了Resource接口定义的方法
public interface Resource extends InputStreamSource {
boolean exist();
boolean isReadable;
boolean isOpen();
boolean isFile();
URL getURL() throws IOException;
URL getURI() throws IOException;
File getFile() throws IOException;
ReadableByteChannel readableChannel() throws IOException;
Resource createRelative(String relativePath) throws IOException;
String getFilename();
String getDescription();
}
UrlResource访问网络资源
1.创建模块
spring6-resource
2.引入依赖
<dependencies>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>2.1.1</version>
</dependency>
<!-- spring context依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.2</version>
</dependency>
<!-- spring-aop依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.0.2</version>
</dependency>
<!-- spring aspects依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>6.0.2</version>
</dependency>
<!-- spring整合Junit相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.2</version>
</dependency>
<!-- junit-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!-- log4j依赖-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.19.0</version>
</dependency>
</dependencies>
3.创建包
com.yogurt.spring6.resource
4.创建类
UrlResourceDemo(演示UrlResource访问网络资源)
//UrlResource访问网络资源
public class UrlResourceDemo {
public static void main(String[] args) {
//访问前缀http
loadUrlResource("http://www.baidu.com");
//访问前缀file
loadUrlResource("file:yogurt.txt");
}
//访问前缀http、file
public static void loadUrlResource(String path){
try {
//创建Resource实现类的对象UrlResource
UrlResource url = new UrlResource(path);
//获取资源信息
System.out.println(url.getFilename());
System.out.println(url.getURI());
System.out.println(url.getDescription());
System.out.println(url.getInputStream().read());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
注意:yogurt.txt文件要在根路径下才可访问到。
ClassPathResourceDemo(演示ClassPathResource访问类路径下资源)
//访问类路径下资源
public class ClassPathResourceDemo {
public static void loadClasspathResource(String path){
//创建对象ClassPathResource
ClassPathResource resource = new ClassPathResource(path);
System.out.println(resource.getFilename());
System.out.println(resource.getDescription());
//获取文件内容
try {
InputStream in = resource.getInputStream();
byte[] b = new byte[1024];
while (in.read(b) != -1){
System.out.println(new String(b));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
loadClasspathResource("yogurt.txt");
}
}
FileSystemResourceDemo(演示FileSystemResource访问类路径下资源)
//访问系统资源
public class FileSystemResourceDemo {
public static void main(String[] args) {
//绝对路径(要先在D盘中创建yogurt.txt文件)
loadFileResource("d:\\yogurt.txt");
//相对路径
loadFileResource("yogurt.txt");
}
public static void loadFileResource(String path){
//创建对象
FileSystemResource resource = new FileSystemResource(path);
System.out.println(resource.getFilename());
System.out.println(resource.getDescription());
try {
InputStream in = resource.getInputStream();
byte[] b = new byte[1024];
while (in.read(b) != -1){
System.out.println(new String(b));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Resource类圈
上述Resource实现类与Resource顶级接口的关系可以用下面的UML关系模型来表示