Spring 3.x企业应用开发实战(3)----资源抽象接口
来源:互联网 发布:手机怎么安装java微信 编辑:程序博客网 时间:2024/05/01 21:10
Spring设计了一个Resource接口,它为应用提供了更强的访问底层资源的能力。该接口拥有对应不同资源类型的实现类。
1、主要的方法
boolean exists()//资源是否存在
boolean isOpen()//资源是否打开
URL getURL()throws IOException//如果底层资源可以表示成URL,该方法返回对应的URL对象。
File getFile() throws IOExcetion//如果底层资源对应一个文件,该方法返回对应的File对象。
InputStream getInputStream() throws IOExcetion//返回资源对应的输入流
2、Resource具体的实现类:
ByteArrayResource:二进制数组表示的资源,二进制数组资源可以在内存中通过程序构造。
ClassPathResource:类路径下的资源,资源以相对于类路径的方式表示。
FileSystemResource:文件系统资源,资源以文件系统路径的方式表示。
InputStreamResource:对应一个InputStream的资源。
ServletContextResource:为访问Web容器上下文中资源而设计的类,负责以对于Web应用根目录的路径加载资源,它支持以流的方式和URL的方式访问。
UrlResource:封装了java.net.URL,它使用户能够访问任何可以通过URL表示的资源,如文件系统的资源、HTTP资源、FTP资源等。
3、一些实例:
package com.techman.resource;import java.io.IOException;import java.io.InputStream;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.FileSystemResource;import org.springframework.core.io.Resource;public class FileSourceExample {public static void main(String []args){try{String filePath="E:/html/sdemo2/webroot/WEB-INF/classes/conf/file1.txt";Resource res1=new FileSystemResource(filePath);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();}}}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>ServletContextResource Demo</title> </head> <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"/> <% Resource res3=new ServletContextResource(application,"/WEB-INF/classes/conf/file1.txt"); out.print(res3.getFilename()+"<br/>"); out.print(WebUtils.getTempDir(application).getAbsolutePath()); %></html>
对于远程服务器的文件系统,用户可以方便地通过UrlResource进行访问。
4、文件编码问题
资源加载时默认采用系统编码读取资源内容,如果资源文件采用特殊的编码格式,那么可以通过EncodedResource对资源进行编码,以保证资源内容的正确性。
package com.techman.resource;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);}}
- Spring 3.x企业应用开发实战(3)----资源抽象接口
- Spring 3.x企业应用开发实战(4)----资源加载
- Spring 3.x 企业应用开发实战
- Spring 3.x企业应用开发实战
- Spring 3.x企业应用开发实战(5)----资源加载器
- Spring 3.x企业应用开发实战(6)----BeanFactory
- Spring 3.x企业应用开发实战(7)----ApplicationContext
- Spring 3.x企业应用开发实战(8)----Bean装配
- Spring 3.x企业应用开发实战(10)----AOP切面
- Spring 3.x企业应用开发实战(14)----事务
- Spring 3.X 企业应用开发实战 -- 目录
- 《Spring.3.x企业应用开发实战》--- 零碎笔记
- Spring 3.x 企业应用开发实战pdf
- Spring 3.x企业实用开发实战
- 关于Spring+3.x企业应用开发实战书中:ant building spring3.x依赖包
- Spring3.x企业应用开发实战-Spring+Hibernat架构分析
- Spring 3.x企业应用开发实战(13)----Spring JDBC访问数据库
- Spring容器高级主题(Spring 3.x企业应用开发实战第五章)学习总结
- Intent调用其他应用setComponent启动其他应用程序
- eclipse git插件配置
- android整合--fragment
- Git 服务器在Ubuntu12.04上的搭建与使用体验第一篇
- 2013杭州网络赛C题HDU 4640(模拟)
- Spring 3.x企业应用开发实战(3)----资源抽象接口
- c语言面试常见问题
- poj1001
- 来北京两个月-发发牢骚
- Java chapter04-1
- 字符串常量到底存放在哪个存储区
- poj1002
- 初学WCF服务以及IIS发布(一)
- 物流追踪 - -GPS和GPRS应用