Spring Resource接口 学习

Resource 接口是 Spring 资源访问策略的抽象,它本身并不提供任何资源访问实现,具体的资源访问由该接口的实现类完成——每个实现类代表一种资源访问策略。Resource一般包括这些实现类:UrlResource、ClassPathResource、FileSystemResource、ServletContextResource、InputStreamResource、ByteArrayResource。

 UrlResource

Resource的一个实现类,用来访问网络资源,它支持URL的绝对路径。

http:------该前缀用于访问基于HTTP协议的网络资源。

ftp:------该前缀用于访问基于FTP协议的网络资源

file: ------该前缀用于从文件系统中读取资源

  测试例子

import org.springframework.core.io.UrlResource;

import java.io.InputStream;

//获取网址的对应信息
public class SpringURLResource {

    public static void getURLResource(String path) {
        try {
            UrlResource urlResource = new UrlResource(path);
            System.out.println(urlResource.getDescription());
            System.out.println(urlResource.getURL());
            System.out.println(urlResource.getFilename());
            InputStream inputStream = urlResource.getInputStream();
            byte[] b = new byte[1024];
            while (inputStream.read(b) != -1) {
               System.out.println(new String(b));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        //网址例子 1   
        getURLResource("http://baidu.com");
        //获取根路径例子 2
        getURLResource("file:hhh.txt");
    }
}

 测试结果:

 ClassPathResource

ClassPathResource 用来访问类加载路径下的资源,无须使用绝对路径访问。(可以理解为他会访问项目中的Resource文件夹中的文件)

   测试例子

import org.springframework.core.io.ClassPathResource;

import java.io.InputStream;

//ClassPathResource
public class SpringClassPathResource {

    public static void getClassPathResource(String path) {
        ClassPathResource classPathResource = new ClassPathResource(path);
        System.out.println(classPathResource.getDescription());
        System.out.println(classPathResource.getPath());
        System.out.println(classPathResource.getFilename());
        try {
            InputStream inputStream = classPathResource.getInputStream();
            byte[] b = new byte[1024];
            while (inputStream.read(b) != -1) {
                System.out.println(new String(b));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void main(String[] args) {
        getClassPathResource("fff.txt");
    }
}

测试结果

 FileSystemResource

 Spring 提供的 FileSystemResource 类用于访问文件系统资源,使用 FileSystemResource 来访问文件系统资源并没有太大的优势,因为 Java 提供的 File 类也可用于访问文件系统资源。(就是可以访问本电脑各个盘中的文件)

测试例子

import org.springframework.core.io.FileSystemResource;

import java.io.InputStream;

//FileSystemResource
public class SpringFileSystemResource {

    public static void getFileSystemResource(String path) {
        FileSystemResource fileSystemResource = new FileSystemResource(path);
        fileSystemResource.getDescription();
        fileSystemResource.getFilename();
        fileSystemResource.getFile();
        try {
            InputStream inputStream = fileSystemResource.getInputStream();
            byte[] b = new byte[1024];
            while (inputStream.read(b) != -1) {
                System.out.println(new String(b));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        getFileSystemResource("C:\\tolen\\www.txt");
    }
}

 测试结果

ServletContextResource、InputStreamResource、ByteArrayResource

*ServletContextResource :这是ServletContext资源的Resource实现,它解释相关Web应用程序根目录中的相对路径。

*InputStreamResource :与其他Resource实现相比,这是已打开资源的描述符。 因此,它的isOpen()方法返回true。如果需要将资源描述符保留在某处或者需要多次读取流,请不要使用它。

 *ByteArrayResource : 字节数组的Resource实现类。通过给定的数组创建了一个ByteArrayInputStream。它对于从任何给定的字节数组加载内容非常有用,而无需求助于单次使用的InputStreamResource。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值