Spring 中的 Resources几个接口实现类描述
1. UrlResource
用于通过 URL 访问资源。支持常见的URL协议,如file:、http:、ftp:等。适用于从网络或文件系统中加载资源。
import org.springframework.core.io.UrlResource;
import java.net.URL;
public class UrlResourceExample {
public static void main(String[] args) throws Exception {
URL url = new URL("https://www.example.com/file.txt");
UrlResource resource = new UrlResource(url);
if (resource.exists()) {
System.out.println("Resource content: " + new String(resource.getInputStream().readAllBytes()));
}
}
}
2. ClassPathResource
用于从类路径加载资源。通常用于加载位于类路径(classpath)中的文件,如配置文件、静态资源等。
适用于在应用内部访问资源。
import org.springframework.core.io.ClassPathResource;
import java.io.InputStream;
public class ClassPathResourceExample {
public static void main(String[] args) throws Exception {
ClassPathResource resource = new ClassPathResource("config/application.properties");
if (resource.exists()) {
InputStream inputStream = resource.getInputStream();
System.out.println("Resource content: " + new String(inputStream.readAllBytes()));
}
}
}
3. FileSystemResource
用于从文件系统加载资源。适用于直接操作文件系统中的文件。支持绝对路径和相对路径。
import org.springframework.core.io.FileSystemResource;
import java.io.File;
public class FileSystemResourceExample {
public static void main(String[] args) throws Exception {
File file = new File("C:/data/example.txt");
FileSystemResource resource = new FileSystemResource(file);
if (resource.exists()) {
System.out.println("Resource content: " + new String(resource.getInputStream().readAllBytes()));
}
}
}
4. ServletContextResource
用于从 Web 应用上下文加载资源(通常在 Servlet 环境中使用)。通常用于访问Web应用中的静态资源,如HTML、CSS、JS文件等。适用于Servlet容器环境。
import org.springframework.core.io.ServletContextResource;
import javax.servlet.ServletContext;
public class ServletContextResourceExample {
public static void main(String[] args) throws Exception {
// 假设在 Servlet 环境中
ServletContext servletContext = ...; // 获取 ServletContext
ServletContextResource resource = new ServletContextResource(servletContext, "/WEB-INF/config.xml");
if (resource.exists()) {
System.out.println("Resource content: " + new String(resource.getInputStream().readAllBytes()));
}
}
}
5. InputStreamResource
用于从输入流加载资源(一次性读取)。适用于已经有一个InputStream的情况,通常用于临时资源。注意:InputStreamResource只能读取一次,不能重复读取。
import org.springframework.core.io.InputStreamResource;
import java.io.ByteArrayInputStream;
public class InputStreamResourceExample {
public static void main(String[] args) throws Exception {
String data = "Hello, InputStreamResource!";
ByteArrayInputStream inputStream = new ByteArrayInputStream(data.getBytes());
InputStreamResource resource = new InputStreamResource(inputStream);
System.out.println("Resource content: " + new String(resource.getInputStream().readAllBytes()));
}
}
6. ByteArrayResource
用于从字节数组加载资源(可重复读取)。适用于在内存中操作的资源,如从字节数组生成资源。可以多次读取。
import org.springframework.core.io.ByteArrayResource;
public class ByteArrayResourceExample {
public static void main(String[] args) throws Exception {
byte[] data = "Hello, ByteArrayResource!".getBytes();
ByteArrayResource resource = new ByteArrayResource(data);
System.out.println("Resource content: " + new String(resource.getInputStream().readAllBytes()));
}
}
总结: