Resource Components of Spring
2016-06-16 16:02
549 查看
1.资源组件UML类图
资源组件接口中核心方法:exists(), isopen(), isReadable(), getInputStream()
引用核心的Java API:File类,URL类
2.重点介绍UrlResource和ClasspathResource
UrlResource
利用统一资源标识符或者定位符加载资源,其中UrlResource封装了Url,UrlConnection的操作。持有Url,Uri等对象。UrlResource的exists(), isReadable()方法继承自父类AbstractResourceResolving。提供了利用Url变量得到文件File。用File类的方法操作资源,验证资源的可读性,是否存在,是否可被打开!通过UrlConnection获取资源的输入流!
例如:
public InputStream getInputStream()
throws IOException {
URLConnection con =
this.url.openConnection();
ResourceUtils.useCachesIfNecessary(con);
try {
return
con.getInputStream();
}
catch (IOException
ex) {
// Close the HTTP connection (if applicable).
if (con
instanceof HttpURLConnection) {
((HttpURLConnection) con).disconnect();
}
throw
ex;
}
}
ClassPathResource
此对象用来获取class path下的资源,其中ClassPathResource持有Class, ClassLoader等对象。利用Class和ClassLoader对象的getResource()和getResourceStream()方法操作资源,妥妥的!
例如:
判断资源是否存在
public
boolean exists() {
return (resolveURL() !=
null);
}
protected URL resolveURL() {
if (this.clazz !=
null) {
return
this.clazz.getResource(this.path);
}
else
if (this.classLoader !=
null) {
return
this.classLoader.getResource(this.path);
}
else {
return ClassLoader.getSystemResource(this.path);
}
}
获取资源的输入流对象
public InputStream getInputStream()
throws IOException {
InputStream is;
if (this.clazz !=
null) {
is =
this.clazz.getResourceAsStream(this.path);
}
else
if (this.classLoader !=
null) {
is =
this.classLoader.getResourceAsStream(this.path);
}
else {
is = ClassLoader.getSystemResourceAsStream(this.path);
}
if (is ==
null) {
throw
new FileNotFoundException(getDescription() +
" cannot be opened because it does not exist");
}
return
is;
}
资源组件接口中核心方法:exists(), isopen(), isReadable(), getInputStream()
引用核心的Java API:File类,URL类
2.重点介绍UrlResource和ClasspathResource
UrlResource
利用统一资源标识符或者定位符加载资源,其中UrlResource封装了Url,UrlConnection的操作。持有Url,Uri等对象。UrlResource的exists(), isReadable()方法继承自父类AbstractResourceResolving。提供了利用Url变量得到文件File。用File类的方法操作资源,验证资源的可读性,是否存在,是否可被打开!通过UrlConnection获取资源的输入流!
例如:
public InputStream getInputStream()
throws IOException {
URLConnection con =
this.url.openConnection();
ResourceUtils.useCachesIfNecessary(con);
try {
return
con.getInputStream();
}
catch (IOException
ex) {
// Close the HTTP connection (if applicable).
if (con
instanceof HttpURLConnection) {
((HttpURLConnection) con).disconnect();
}
throw
ex;
}
}
ClassPathResource
此对象用来获取class path下的资源,其中ClassPathResource持有Class, ClassLoader等对象。利用Class和ClassLoader对象的getResource()和getResourceStream()方法操作资源,妥妥的!
例如:
判断资源是否存在
public
boolean exists() {
return (resolveURL() !=
null);
}
protected URL resolveURL() {
if (this.clazz !=
null) {
return
this.clazz.getResource(this.path);
}
else
if (this.classLoader !=
null) {
return
this.classLoader.getResource(this.path);
}
else {
return ClassLoader.getSystemResource(this.path);
}
}
获取资源的输入流对象
public InputStream getInputStream()
throws IOException {
InputStream is;
if (this.clazz !=
null) {
is =
this.clazz.getResourceAsStream(this.path);
}
else
if (this.classLoader !=
null) {
is =
this.classLoader.getResourceAsStream(this.path);
}
else {
is = ClassLoader.getSystemResourceAsStream(this.path);
}
if (is ==
null) {
throw
new FileNotFoundException(getDescription() +
" cannot be opened because it does not exist");
}
return
is;
}
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- Enterprise Library for .NET Framework 2.0缓存使用实例
- C# Entity Framework中的IQueryable和IQueryProvider详解
- 由vbs sort引发.NET Framework之间的关系说明
- C#修改IIS站点framework版本号的方法
- C#难点逐个击破(6):C#数据类型与.net framework数据类型
- zend framework框架中url大小写问题解决方法
- YII Framework框架教程之安全方案详解
- Yii PHP Framework实用入门教程(详细介绍)
- 微软NET Framework 3.5 Beta 1 发布 提供下载
- 使用Entity Framework(4.3.1版本)遇到的问题整理
- 模拟Spring的简单实现