您的位置:首页 > 编程语言 > Java开发

Spring -- 资源访问(二)

2017-04-01 09:27 183 查看
知识点的梳理:

Resource接口以及实现类可以脱离Spring框架使用,它比通过JDK访问资源的API好用很多;
  

资源抽象接口

Spring设计了Resource接口,用来访问底层资源路径;
Resource在Spring中非常重要,Spring使用Resource装载各种资源,这些资源包括配置文件资源,国际化属性文件等;
通过它,可以将Spring的配置信息放置在任何地方(如数据库,LDAP中),只要最终可以通过Resource接口返回配置信息就可以了;

常用方法:
boolean exists():资源是否存在;
boolean isOpen():资源是否打开;
URL getURL() throws IOException:如果底层资源可以表示成URL,该方法返回对应的URL对象;
File getFile() throws IOExceion:如果底层资源对应一个文件,该方法返回对应的File对象;
InputStream getInputStream() throws IOException:返回资源对应的输入流;

Resource的具体实现类

图解



类的解析:
ByteArrayResource:二进制数组表示的资源,二进制数组资源可以在内存中通过程序构造;
ClassPathResource:类路径下的资源,资源以相对于类路径的方式表示;
FileSystemResource:文件系统资源,资源以文件系统路径的方式表示,如:"D:/conf/bean.xml";
InputStreamResource:对应一个InputStream的资源;
ServletContextResource:为访问Web容器上下文中的资源而设计的类,负责以相对于Web应用根目录的路径加载资源,它支持以流和URL的方式访问,在WAR解包的情况下,也可以通过File的方式访问,该类还可以直接从JAR包中访问资源;
UrlResource:封装了java.net.URL,它使用户能够访问任何可以通过URL表示的资源,如文件系统的资源,HTTP资源,FTP资源;

说明:假设有一个文件位于Web应用的类路径下,可通过如下方式访问:
FileSystemResource:以文件系统绝对路径的方式进行访问;
ClassPathResource:以类路径的方式进行访问;
ServletContextResource:以相对于Web应用根目录的方式进行访问;

实例1:通过FileSystemResource和ClassPathResource访问同一个文件资源:

public class FileSourceExample {

public static void main(String[] args) {
try {
String filePath = "D:/masterSpring/chapter3/WebRoot/WEB-INF/classes/conf/file1.txt";
  
//1.使用系统文件路径方式加载文件
Resource res1 = new FileSystemResource(filePath);
//2.使用类路径方式加载文件
Resource res2 = new ClassPathResource("conf/file1.txt");

 

InputStream ins1 = res1.getInputStream();

InputStream ins2 = res2.getInputStream();

System.out.println("res1:"+res1.getFilename());

System.out.println("res2:"+res2.getFilename());

} catch (IOException e) {
e.printStackTrace();
}
}
}

获取资源后,可通过Resource接口定义的多个方法访问文件的资源:
getFileName():获取文件名;
getFile():获取资源对应的File对象;
getInputStream():直接获取文件的输入流;
createRelative(String relativePath):在资源相对地址上创建新的文件;

示例2:在WEB应用中,用户可以通过ServletContextResource以相对于Web应用根目录的方式访问文件资源

resource.jsp文件

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

<jsp:directive.page import="org.springframework.web.context.support.ServletContextResource"/>

<jsp:directive.page import="org.springframework.core.io.Resource"/>

<jsp:directive.page import="org.springframework.web.util.WebUtils"/>

<%

//注意文件资源地址以相对于WEB应用根路径的方式表示

Resource res3 = new ServletContextResource(application,"/WEB-INF/classes/conf/file1.txt");

out.print(res3.getFilename()+"<br/>");

out.print(WebUtils.getTempDir(application).getAbsolutePath());

%>

示例3:资源加载时默认采用系统编码读取资源内容,如果资源文件采用特殊的编码格式,那么可以通过EncodedResource对资源进行编码:

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

import org.springframework.core.io.support.EncodedResource;

import org.springframework.util.FileCopyUtils;

public class EncodedResourceExample {

public static void main(String[] args)throws Throwable {
Resource res = new ClassPathResource("conf/file1.txt");
EncodedResource encRes = new EncodedResource(res,"UTF-8");
String content = FileCopyUtils.copyToString(encRes.getReader());
System.out.println(content);
}
}

资源加载

为了访问不同类型的资源,必须使用相应的Resource实现类,还是比较麻烦的;
Spring可以通过"classpath:","file"等资源地址前缀识别不同的资源类型;

资源地址表达式:
Spring支持的资源类型地址前缀

地址前缀

示例

对应资源类型

classpath:

classpath:com/baobaotao/beanfactory/beans.xml

从类路径中加载资源,classpath:和classpath:/是等价的,都是相对于类的根路径。资源文件可以在标准的文件系统中,也可以在jar或zip的类包中

classpath*:

  

如果有多个文件系统路径拥有相同的包名(如,com.baobaotao),"classpath:"只会在第一个加载的com.baobaotao包下查找。而"classpath*:"会扫描所有的com.baobaotao类路径

file:

file :/comf/com/baobaotao/beanfactory/beans.xml

使用UrlResource从文件系统目录中装载资源,可采用绝对或相对路径

http://
http ://www.baobaotao.com/resource/beans.xml

使用UrlResource从web服务器中装载资源

ftp://
ftp: //www.baobaotao.com/resource/beans.xml

使用UrlResource从FTP服务器装载资源

没有前缀

com/baobaotao/beanfactory/beans.xml

根据ApplicationContext具体实现类采用对应的类型的Resource

Ant风格资源地址支持三种匹配符:
?:匹配文件名中的一个字符;
*:匹配文件名中任意个字符;
**:匹配多层路径;

Ant风格的资源路径示例:
classpath:com/t?st.xml:匹配con类路径下com/test.xml,com/tast.xml或者com/txst.xml;
file :D:/conf/*/xml:匹配文件系统D:/conf目录下所有以XML为后缀的文件;
classpath:com/**/test.xml:匹配con类路径下(当前目录及其子孙目录)的test.xml文件;
classpath:org/springframework/**/*.xml:匹配类路径org/springframework下所有以xml为后缀的文件;
classpath:org/**/servlet/bla.xml:匹配类路径org/springfamework/servlet/bla.xml,也匹配org/springframework/testing/servlet/bla.xml,还匹配org/servlet/bla.xml

资源加载器

Spring定义了一套资源加载的接口,并提供了实现类;



ResourceLoader
ResourcePatternResolver扩展ResourceLoader接口,定义了一个新的接口方法getResource(String locationPattern),此方法支持带资源类型前缀及ANT表达式;
PathMatchingResourcePatternResolver是Spring提供的标准实现类

示例:

import org.springframework.core.io.Resource;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.core.io.support.ResourcePatternResolver;

  

public class PatternResolverTest {

  

public static void main(String[]args)
throws Throwable{
ResourcePatternResolverresolver =
new PathMatchingResourcePatternResolver();
//加载所有类包com.baobaotao(及子孙包)下的以XML为后缀的资源
Resourceresources[] =resolver.getResources("classpath*:com/baobaotao/**/*.xml");
for(Resourceresource:resources){
System.out.println(resource.getDescription());
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring java