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

好记性不如烂笔头82-spring3学习(3)-spring的Resource使用和传统方法的对比

2015-03-18 12:15 369 查看
部分文字和说明来自网上

[code]ResourceLoader接口,用于返回Resource对象;其实现可以看作是一个生产Resource的工厂类
 核心方法:
  #Resource getResource(String location);
  location: "classpath:path"表示返回ClasspathResource,"http://path"和"file:path"表示返回UrlResource资源,不加前缀则需要根据当前上下文来决定。

Spring定义的所有ApplicationContext都实现了ResourceLoader
     ClassPathXmlApplicationContext:不指定前缀将返回默认的ClassPathResource资源,否则将根据前缀来加载资源;
     FileSystemXmlApplicationContext:不指定前缀将返回FileSystemResource,否则将根据前缀来加载资源;
     WebApplicationContext:不指定前缀将返回ServletContextResource,否则将根据前缀来加载资源;
     其他:不指定前缀根据当前上下文返回Resource实现,否则将根据前缀来加载资源。

ResourceLoaderAware接口
 标记接口,用于通过ApplicationContext上下文注入ResourceLoade;
 void setResourceLoader(ResourceLoader resourceLoader); 

使用路径通配符加载Resource
 #通配符
 "?":匹配一个字符,如"config?.xml"将匹配"config1.xml";
 "*":匹配零个或多个字符串,如"cn/*/config.xml"将匹配"cn/com/config.xml",但不匹配匹配"cn/config.xml";而"cn/config-*.xml"将匹配"cn/config-service.xml";
 "**":匹配路径中的零个或多个目录,如"cn/**/config.xml"将匹配"cn/config.xml",也匹配"cn/com/spring/config.xml";而"cn/com/config-**.xml"将匹配"cn/com/config-service.xml",即把"**"当做两个"*"处理。


[code] #加载多个Resource:ResourcePatternResolver接口加载多个Resource,Resource[] getResources(String locationPattern) throws IOException;
   |*|classpath,加载类路径(包括jar包)中的一个且仅一个资源
   Resource[] resources=resolver.getResources("classpath:cn/tt.txt");//加载一个匹配的文件tt.txt
   Resource[] resources=resolver.getResources("classpath:cn/*.txt");   //即使有多个,也仅加载一个匹配的文件
   |*|classpath*,加载类路径(包括jar包)中的所有匹配的资源
   使用"ClassLoader"的"Enumeration<URL> getResources(String name)"方法来查找通配符之前的资源,然后通过模式匹配来获取匹配的资源。
   如"classpath*:source/*.txt"将首先加载通配符之前的目录"source",然后再遍历路径进行子路径匹配从而获取匹配的资源*.txt
   说明:如果要返回jar包里面的资源文件,通配符之前必须有目录,否则就只加载classpath的资源。由于classLoader的getResources(String name)方法的限制,对于name为空将只返回文件系统的类路径,不会包换jar包根路径
   |*|file*,加载一个或多个文件系统中的Resource。如"file:D:/*.txt"将返回D盘下的所有txt文件
   |*|无前缀:通过ResourceLoader实现加载一个资源,AppliacationContext提供的getResources方法将获取资源委托给ResourcePatternResolver实现

AppliacationContext实现对各种Resource的支持

 #ClassPathXmlApplicationContext,通过classpath进行加载返回ClassPathResource 
 #FileSystemXmlApplicationContext,加载相对于当前工作目录的"configLocation"位置的资源
部分简单源代码
 //linux系统,第一个将相对于当前vm路径进行加载;  第二个则是绝对路径方式加载  
 ctx.getResource ("tt/config.xml");  
 ctx.getResource ("/home/confg.xml");  
 //windows系统,第一个将相对于当前vm路径进行加载;第二个则是绝对路径方式加载  
 ctx.getResource ("tt/config.xml");  
 ctx.getResource ("d:/tt/confg.xml");


SPRING3中Resource的使用,以及和传统方法的对比

[code]package com.spring;

import java.io.*;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.util.FileCopyUtils;
/**  
 * 简单说明Spring3 的Resource的使用,以及和传统方法的对比
 *  
 * @author 范芳铭
 */  
public class EasyFileSource {
    public static void main(String[] args){
        try{
            //我本地的绝对路径
            String filePath = "D:/develop/eclipse/work/webStudy/WebRoot/WEB-INF/classes/ffm_file.txt";

            //使用传统的系统文件路径加载文件
            Resource resOld = new FileSystemResource(filePath);

            //使用spring3 提供的类路径加载文件方式,相对路径的方式,在实际部署中优势明显
            Resource resNew = new ClassPathResource("ffm_file.txt");

            InputStream insOld = resOld.getInputStream();
            InputStream insNew = resNew.getInputStream();

            //输出文件名称
            System.out.println("res old:" + resOld.getFilename());
            System.out.println("res new:" + resNew.getFilename());

            //利用传统方法输出文件内容  
            byte b[] = new byte[1024];   
            int len = 0;   
            int temp=0;          //所有读取的内容都使用temp接收   
            while((temp=insNew.read())!=-1){    //当没有读取完时,继续读取   
                b[len]=(byte)temp;   
                len++;   
            }   
            insNew.close();   
            System.out.println("--利用传统方法输出 内容");
            System.out.println(new String(b,0,len));   

            //利用新办法输出,对编码等解析方便
            EncodedResource encRes = new EncodedResource(resNew,"UTF-8");
            String content = FileCopyUtils.copyToString(encRes.getReader());
            System.out.println("--利用新办法输出 内容");
            System.out.println(content);

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

}


运行情况

[code]res old:ffm_file.txt
res new:ffm_file.txt
--利用传统方法输出 内容
hi,every one.
浣犲ソ锛屾垜鏄腑鏂囥?
--利用新办法输出 内容
hi,every one.
你好,我是中文。


利用Resource对中文乱码的处理

[code]//利用新办法输出,对编码等解析方便
            EncodedResource encRes = new EncodedResource(resNew,"UTF-8");
            String content = FileCopyUtils.copyToString(encRes.getReader());
            System.out.println("--利用新办法输出 内容");
            System.out.println(content);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐