Spring之Resource类

1、简介:

本质上就是java.io.File的封装

根据资源位置的不同,提供了不同的实现类,用来快速的获取文件资源。

FileSystemResource    获取磁盘上的文件

ClassPathResource     获取类路径的文件

UrlResource                 获取网络上的文件

InputStreamResource  获取输入流上的文件

2、基本用法

package ioc21;

import jdk.internal.util.xml.impl.Input;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


public class Test {
    public static void main(String [] args) throws IOException {
        //从磁盘读取
        //Resource resource = new FileSystemResource("E:/tmp/test.txt");
        //从类路径下读取
        Resource resource = new ClassPathResource("ioc21/spring.xml");
        
        System.out.println(resource.getFilename());
        System.out.println(resource.contentLength());
        InputStream inputStream = resource.getInputStream();
        //从输入流读到输出流   相当于文件复制
        StreamUtils.copy(inputStream,new FileOutputStream("E:/tmp/test1.xml"));

    }
}

3、装配Resource

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean id="springBean" class="ioc21.SpringBean">
            <!--文件系统下的资源用file,表示file协议-->
            <property name="resource" value="file:E:/tmp/test.txt"/>
            <!--类路径下的资源用classpath,表示classpath协议-->
            <!--<property name="resource" value="classpath:ioc21/spring.xml"/>-->
        </bean>
</beans>
package ioc21;


import org.springframework.core.io.Resource;

public class SpringBean {
    private Resource resource;

    public Resource getResource() {
        return resource;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }
}
package ioc21;

import jdk.internal.util.xml.impl.Input;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


public class Test {
    public static void main(String [] args) throws IOException {
        //从磁盘读取
        //Resource resource = new FileSystemResource("E:/tmp/test.txt");
        //从类路径下读取
        //Resource resource = new ClassPathResource("ioc21/spring.xml");

        //通过注入的方式获取资源文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("ioc21/spring.xml");
        SpringBean springBean = (SpringBean) ac.getBean("springBean");
        Resource resource = springBean.getResource();
        System.out.println(resource.getFilename());
        System.out.println(resource.contentLength());
        InputStream inputStream = resource.getInputStream();
        //从输入流读到输出流   相当于文件复制
        StreamUtils.copy(inputStream,new FileOutputStream("E:/tmp/test1.xml"));



    }
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值