Spring 3.x企业应用开发实战(3)----资源抽象接口

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、一些实例:

 

[java]  view plain copy print ?
 
  1. <span style="font-family:Microsoft YaHei; font-size:14px">package com.techman.resource;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5.   
  6. import org.springframework.core.io.ClassPathResource;  
  7. import org.springframework.core.io.FileSystemResource;  
  8. import org.springframework.core.io.Resource;  
  9.   
  10. public class FileSourceExample   
  11. {  
  12.     public static void main(String []args)  
  13.     {  
  14.         try{  
  15.             String filePath="E:/html/sdemo2/webroot/WEB-INF/classes/conf/file1.txt";  
  16.               
  17.             Resource res1=new FileSystemResource(filePath);  
  18.             Resource res2=new ClassPathResource("conf/file1.txt");  
  19.               
  20.             InputStream ins1=res1.getInputStream();  
  21.             InputStream ins2=res2.getInputStream();  
  22.               
  23.             System.out.println("RES1:"+res1.getFilename());  
  24.             System.out.println("RES2:"+res2.getFilename());  
  25.         }  
  26.         catch(IOException e)  
  27.         {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31. }</span>  

 

[java]  view plain copy print ?
 
  1. <span style="font-family:Microsoft YaHei; font-size:14px"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.   <head>      
  5.     <title>ServletContextResource Demo</title>  
  6.   </head>  
  7.     
  8.     <jsp:directive.page import="org.springframework.web.context.support.ServletContextResource"/>  
  9.     <jsp:directive.page import="org.springframework.core.io.Resource"/>  
  10.     <jsp:directive.page import="org.springframework.web.util.WebUtils"/>  
  11.       
  12.     <%  
  13.         Resource res3=new ServletContextResource(application,"/WEB-INF/classes/conf/file1.txt");  
  14.         out.print(res3.getFilename()+"<br/>");  
  15.         out.print(WebUtils.getTempDir(application).getAbsolutePath());  
  16.      %>  
  17. </html></span>  

对于远程服务器的文件系统,用户可以方便地通过UrlResource进行访问。

4、文件编码问题

资源加载时默认采用系统编码读取资源内容,如果资源文件采用特殊的编码格式,那么可以通过EncodedResource对资源进行编码,以保证资源内容的正确性。

 

[java]  view plain copy print ?
 
  1. <span style="font-family:Microsoft YaHei; font-size:14px">package com.techman.resource;  
  2.   
  3. import org.springframework.core.io.ClassPathResource;  
  4. import org.springframework.core.io.Resource;  
  5. import org.springframework.core.io.support.EncodedResource;  
  6. import org.springframework.util.FileCopyUtils;  
  7.   
  8. public class EncodedResourceExample   
  9. {  
  10.     public static void main(String []args)throws Throwable  
  11.     {  
  12.         Resource res=new ClassPathResource("conf/file1.txt");  
  13.         EncodedResource encRes=new EncodedResource(res,"UTF-8");  
  14.         String content=FileCopyUtils.copyToString(encRes.getReader());  
  15.         System.out.println(content);          
  16.     }  
  17. }</span><strong style="font-size:18px">  
  18. </strong>  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值