转载自:https://www.2cto.com/kf/201803/731102.html
JDK提供的访问资源的类(File等)不能很好满足各种某些资源的访问需求。比如缺少从类路径和Web容器的上下文中获取资源的资源操作类。Spring的Resource接口提供了更好用的资源访问能力。Spring使用Resource访问各种资源文件,配置文件资源,国际化属性资源等。
Resource.java
Resource 继承了 InputStreamSource
package org.springframework.core.io;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import org.springframework.core.io.InputStreamSource;
public interface Resource extends InputStreamSource {
boolean exists();//资源是否存在
boolean isReadable();
boolean isOpen();//资源是否打开
URL getURL() throws IOException;//如果底层资源可以表示成URL,则返回对应的URL对象
URI getURI() throws IOException;
File getFile() throws IOException;//如果底层资源是一个文件,则返回对应的File对象
long contentLength() throws IOException;
long lastModified() throws IOException;
Resource createRelative(String var1) throws IOException;
String getFilename();
String getDescription();
}
InputStreamSource .java
package org.springframework.core.io;
import java.io.IOException;
import java.io.InputStream;
public interface InputStreamSource {
InputStream getInputStream() throws IOException;//返回资源对应的输入流
}
介绍两个最常用的Resource实现类:
WritableResource:可写资源接口。两个实现类PathResource和FileSystemResourceClassPathResource:用于加载类路径下的资源。当资源是在项目里的时候,用这个,相对路径。FileSystemResource:用于加载文件系统资源,如:D:/conf/test.xml ,绝对路径。UrlResource:访问任何可以通过URL表示的资源,包括文件系统资源、HTTP资源、FTP资源PathResource:可以访问任何通过URL、Path、系统文件路径表示的资源
资源访问Demo – FileSourceExample .java
package springResources;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.core.io.support.EncodedResource;
import java.io.*;
/**
* Created by Mistra-WR on 2018/3/18/018.
*/
public class FileSourceExample {
public static void main(String [] args){
try {
String filePath = "D:/Working/Spring/src/main/resources/conf/resourceFileTest.txt";
//使用系统文件路径方式加载文件
WritableResource res1 = new PathResource(filePath);
//使用类路径方式加载文件
Resource res2 = new ClassPathResource("conf/resourceFileTest.txt");
EncodedResource encRes = new EncodedResource(res2,"UTF-8");//可以对资源进行特定编码
File file1 = res1.getFile();//获得File对象
File file2 = encRes.getResource().getFile();//获得File对象
OutputStream stream1 = res1.getOutputStream();//获取文件输出流
stream1.write("我是那天空飘荡的风筝".getBytes());
stream1.close();
//使用Resource接口读资源文件
InputStream ins1 = res1.getInputStream();//获取文件输入流
InputStream ins2 = res2.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
while ((i = ins1.read())!=-1){
baos.write(i);
}
System.out.println(baos.toString());
System.out.println("res1:" + res1.getFilename());//获得文件名
System.out.println("res2:" + res2.getFilename());
}catch (IOException e){
e.printStackTrace();
}
}
}
/* Output:
我是那天空飘荡的风筝
res1:resourceFileTest.txt
res2:resourceFileTest.txt
*///:~
资源类型的地址前缀
classpath:classpath:com/smart/beans.xml 从类路径中加载资源file:file:/conf/com/smart/beans.xmlUrlResource从文件系统目录中装载资源,可采用绝对或相对路径 https://:https://www.smart.com/resource/beans.xmlUrlResource从Web服务器中装载资源 ftp://:ftp://www/smartcom/resource/beans.xmlUrlResource从FTP服务器中装载资源 没有前缀: 根据ApplicationContext进行判断采用对应类型的Resourceclasspath:和classpath*:的区别:主要在于多个模块打包的应用时的区别。具体参考:Spring加载resource时classpath*:与classpath:的区别
Ant风格的资源地址支持3种匹配符
?:匹配文件名中的一个字符*:匹配文件名中的任意字符**:匹配多层路径
资源加载Demo – PatternResolverTest .java
package springResources;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.testng.annotations.Test;
/**
* Created by Mistra-WR on 2018/3/20/020.
*/
public class PatternResolverTest {
@Test
public void getResources() throws Throwable{
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource resource[] = resolver.getResources("classpath*:com/smart/**/*.xml");
for (Resource resource1:resource){
System.out.println(resource1.getDescription());
}
}
}
/* Output:
file [D:\Spring4\target\classes\com\smart\xxx.xml]
...
*///:~
PathMatchingResourcePatternResolver将扫描虽有类路径下及JAR包中对应com.smart类包下的路径,读取所有以.xml为后缀的文件资源。
401



被折叠的 条评论
为什么被折叠?



