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