Spring之Resource

Spring之Resource

本文讲解如何使用Spring中的Resource接口处理资源

Resource接口的几个主要实现类如下:

  • UrlResource

  • ClassPathResource

  • FileSystemResource

  • PathResource

  • ServletContextResource

  • InputStreamResource

  • ByteArrayResource

UrlResource

访问本地资源

public static void main(String[] args) throws IOException {
    Resource resource =
           new UrlResource("file:\\C:\\data\\test.txt");
    System.out.println(resource.getFile());
}

在这里插入图片描述

访问网络资源

使用springboot设置一个网络文本资源

在这里插入图片描述

访问网络资源需要通过getInputStream()获取流

public static void main(String[] args) throws IOException {
    
    Resource resource =
            new UrlResource("http://localhost:8080/test.txt");
    InputStream is = resource.getInputStream();
    byte[] txt = new byte[is.available()];
    System.out.println(is.read(txt));
    System.out.println(new String(txt));
}

在这里插入图片描述

也可以访问ftp协议资源(不便演示,做法相同)

ClassPathResource

此类表示应从类路径获取的资源。它使用线程上下文类加载器、给定的类加载器或给定的类来加载资源。

public static void main(String[] args) {
    Resource resource = new ClassPathResource("test.txt");
    System.out.println(resource.getFilename());
}

在这里插入图片描述

FileSystemResource

Spring 提供的 FileSystemResource 类用于访问文件系统资源,使用 FileSystemResource 来访问文件系统资源并没有太大的优势,因为 Java 提供的 File 类也可用于访问文件系统资源。

当然使用 FileSystemResource 也可消除底层资源访问的差异,程序通过统一的 Resource API 来进行资源访问。下面程序是使用 FileSystemResource 来访问文件系统资源的示例程序。

访问classpath下的资源

public static void main(String[] args) {
    Resource resource = new FileSystemResource("test.txt");
    System.out.println(resource.getFilename());
}

在这里插入图片描述

访问本地磁盘下的资源

public static void main(String[] args) throws IOException {

    Resource resource = new FileSystemResource("c:\\data\\test.txt");
    System.out.println(resource.getFile());
}

在这里插入图片描述

PathResource

读取本地资源

public static void main(String[] args) throws IOException {
    Resource resource = new PathResource("c:\\data\\test.txt");
    System.out.println(resource.getFile());
}

在这里插入图片描述

读取classpath下的资源

public static void main(String[] args) throws IOException {
    Resource resource = new PathResource("test.txt");
    System.out.println(resource.getFile());
}

在这里插入图片描述

ServletContextResource

该资源实现类作用是从web应用程序上下文中获取资源

创建一个SpringBoot接口如下:

@RestController
public class ServletContextResourceController {


    @GetMapping("/test")
    public String test(HttpServletRequest request) {
        ServletContext servletContext = request.getServletContext();
        Resource resource = new ServletContextResource(servletContext, "test.txt");
        return resource.getFilename();
    }

}

访问接口获取资源文件名返回

在这里插入图片描述

InputStreamResource

获取一个输入流,通过输入流获取资源

public static void main(String[] args) throws IOException {
     // 获取文件对象
     File file = new File("c:\\data\\test.txt");
     // 获取文件输入流
     FileInputStream fis = new FileInputStream(file);
     // 模拟通过输入流
     Resource resource = new InputStreamResource(fis);
     System.out.println(resource.contentLength());

th());

}

在这里插入图片描述

ByteArrayResource

通过获取一个字节数组来加载资源

public static void main(String[] args) throws IOException {
    String txt = "测试ByteArrayResource";
    Resource resource = new ByteArrayResource(txt.getBytes(StandardCharsets.UTF_8));
    System.out.println(resource.contentLength());
}

在这里插入图片描述

Spring之ResourceLoader

我们通过Spring的应用上下文获取资源加载加载各种方式的资源

public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext();

    Resource resource = ctx.getResource("classpath:text.txt");

    System.out.println(resource.getFilename());
}

如上代码,实例化ApplicationContext,通过getResource()方法加载classpath类路径下的资源文件,classpath可以省略不写。

从文件系统中加载资源

public static void main(String[] args) {
     ApplicationContext ctx = new ClassPathXmlApplicationContext();

	 // Resource resource = ctx.getResource("file:\\c:\\data\\test.txt");
     Resource resource = ctx.getResource("c:\\data\\test.txt");

     System.out.println(resource.getFilename());
}

加载(http)网络资源

public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext();

    Resource resource = ctx.getResource("http://lcoalhost:8080/test.txt");

    System.out.println(resource.getFilename());
}

Spring之ResourcePatternResolver

从源码中得知,该接口继承了上一节中的ResourceLoader

源码如下:

public interface ResourcePatternResolver extends ResourceLoader {
    String CLASSPATH_ALL_URL_PREFIX = "classpath*:";

    Resource[] getResources(String locationPattern) throws IOException;
}

添加了一个getResources方法,能够获取多个Resource资源对象

而ApplicationContext接口又继承该接口,因此ApplicationContext接口也具备了加载资源的能力。

public static void main(String[] args) throws IOException {
    ApplicationContext ctx = new ClassPathXmlApplicationContext();

    Resource[] resources = ctx.getResources("test*.txt");
    for (Resource resource : resources) {
        System.out.println(resource.getFilename());
    }
}

在这里插入图片描述在这里插入图片描述

我们通过 * 通配符匹配test前缀的文件,都将加载进来,返回一个资源对象数组。

Spring之ResourceLoaderAware接口

Spring程序中,通过实现ResourceLoaderAware接口可以注入ResourceLoader资源加载器,而我们的ApplicationContext应用上下文继承了ResoucerLoader接口,因此他本身就是一个ResourceLoader资源加载器。

public class TestResourceLoaderAware {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);

        MyResource bean = ctx.getBean(MyResource.class);
        System.out.println(bean.getResourceLoader());
    }

}

@Configuration
class Config {

    @Bean
    public MyResource myResource() {
        return new MyResource();
    }

}

class MyResource implements ResourceLoaderAware {

    private ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public ResourceLoader getResourceLoader() {
        return resourceLoader;
    }
}

如上述代码,我们创建MyResource类实现ResourceLoaderAware接口,获取ResourceLoader对象,应用上下通过加载Spring注解配置类创建ApplicationContext对象,获取MyResource这个Bean,并打印他的ResourceLoader属性,结果如下:

在这里插入图片描述

如图他就是我们的ApplicationContext对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值