Spring 中的 Resources几个接口实现类描述

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()));
    }
}

总结:

UrlResource:通过 URL 访问资源。

ClassPathResource:从类路径加载资源。

FileSystemResource:从文件系统加载资源。

ServletContextResource:从 Web 应用上下文加载资源。

InputStreamResource:从输入流加载资源(一次性读取)。

ByteArrayResource:从字节数组加载资源(可重复读取)。

每个示例都展示了如何创建和读取资源的内容,你可以根据实际需求选择合适的实现类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值