Resource(2)

ResourceUtil

ResourceUtil.java

package com.test;

import java.io.IOException;
import java.io.InputStream;

import org.springframework.core.io.Resource;

public class ResourceUtil {
private static InputStream is=null;
public static void stream(Resource resource ){
    try {
        //获取文件资源输入流
        is=resource.getInputStream();
        //读取资源
        byte[] byteArray=new byte[is.available()];
        is.read(byteArray);
        System.out.println(new String(byteArray));
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally{
        try {
        //关闭流
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

ByteArrayResource

在ByteArrayResource中可以知道getInputStream()返回的是一个ByteArrayInputStream

ByteArrayResource.class

  public InputStream getInputStream()
    throws IOException
  {
    return new ByteArrayInputStream(this.byteArray);
  }

ByteArrayResourceTest .java

package com.test;

import org.junit.Test;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;

public class ByteArrayResourceTest {
    @Test
    public void byteArrayResourceTest(){
        Resource resource=new ByteArrayResource("Hello".getBytes());
        if(resource.exists()){
            ResourceUtil.stream(resource);
        }

    }
}

InputStreamResource

InputStreamResource的getInputStream()返回的是一个InputStream
并且this.read设置为true,即流只能读一次

InputStreamResource.class

  public InputStream getInputStream()
    throws IOException, IllegalStateException
  {
    if (this.read) {
      throw new IllegalStateException("InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times");
    }

    this.read = true;
    return this.inputStream;
  }

InputStreamResourceTest.java

package com.test;

import java.io.ByteArrayInputStream;

import org.junit.Test;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;

public class InputStreamResourceTest {
    @Test
    public void inputStreamResourceTest(){
        ByteArrayInputStream inputStream=new ByteArrayInputStream("Hello!!!".getBytes());

        Resource resource=new InputStreamResource(inputStream); 
        if(resource.exists()){
            ResourceUtil.stream(resource);
        }

    }
}

FileSystemResource

FileSystemResource的getInputStream()返回的是一个FileInputStream字节流
并且不限读次数

FileSystemResourceTest.java

package com.test;

import java.io.File;

import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class FileSystemResourceTest {
    @Test
    public void  FileSystemResourceTest(){
        File file=new File("d:/a.txt"); 
        Resource resource=new FileSystemResource(file); 
        if(resource.exists()){
            ResourceUtil.stream(resource);      
        }

    }
}

ClassPathResource

三个构造方法替代了Class类和ClassLoader类的加载类路径资源的方法

  • public ClassPathResource(String path)
    使用的是默认的ClassLoader(ClassUtils.getDefaultClassLoader())加载“path”类路径资源;

  • public ClassPathResource(String path, ClassLoader classLoader)
    使用指定的ClassLoader加载“path”类路径资源,例如:比如当前类路径为”com.fsl.test”,需要加载的资源路径为”com/fsl/test.properties”,则将加载的资源在”com/fsl/test.properties”;

  • public ClassPathResource(String path, Class<?> clazz)
    使用指定类加载”path”类路径资源,例如:比如当前类路径为”com.fsl.test”,需要加载的资源路径为”com/fsl/test.properties”,则将加载的资源在”com/fsl/com/fsl/test.properties”;若需要加载资源路径为“test.properties”,则将加载的资源在”com/fsl/test.properties”

ClassPathResource.class

  public ClassPathResource(String path)
  {
    this(path, (ClassLoader)null);
  }

  public ClassPathResource(String path, ClassLoader classLoader)
  {
    Assert.notNull(path, "Path must not be null");
    String pathToUse = StringUtils.cleanPath(path);
    if (pathToUse.startsWith("/")) {
      pathToUse = pathToUse.substring(1);
    }
    this.path = pathToUse;
    this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
  }

  public ClassPathResource(String path, Class<?> clazz)
  {
    Assert.notNull(path, "Path must not be null");
    this.path = StringUtils.cleanPath(path);
    this.clazz = clazz;
  }
  • 使用默认的ClassLoader

ClassPathResourceTest.java

package com.test;

import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ClassPathResourceTest {
    @Test
    public void  FileSystemResourceTest(){
        Resource resource=new ClassPathResource("com/test/test.properties");
        if(resource.exists()){
            ResourceUtil.stream(resource);      
        }

    }
}
  • 使用指定的ClassLoader

ClassPathResourceTest.java

package com.test;

import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ClassPathResourceTest {
    @Test
    public void  FileSystemResourceTest(){
        ClassLoader cl=this.getClass().getClassLoader();
        Resource resource=new ClassPathResource("com/test/test.properties",cl);
        if(resource.exists()){
            ResourceUtil.stream(resource);      
        }

    }
}
  • 使用指定类,即相对于指定类的路径

ClassPathResourceTest.java

package com.test;

import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ClassPathResourceTest {
    @Test
    public void  FileSystemResourceTest(){
        Class c=this.getClass();
        Resource resource=new ClassPathResource("test.properties",c);
        if(resource.exists()){
            ResourceUtil.stream(resource);      
        }

    }
}
  • 加载jar包里的资源,当在当前路径找不到后,才到jar包里找,并且返回第一个在jar包里找到的资源

ClassPathResourceTest.java

package com.test;

import java.io.File;

import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ClassPathResourceTest {
    @Test
    public void  FileSystemResourceTest(){
        Resource resource=new ClassPathResource("test.properties");
        if(resource.exists()){
            ResourceUtil.stream(resource);      
        }

    }
}



能在当前类路径找到
输出结果
这里写图片描述
System.out.println(resource.getURL());
打印file:/F:/workspace-nyj/Spring_1/bin/test.properties


然后删除此test.properties,随便在一个jar包中添加test.properties用于测试
这里写图片描述
输出结果
这里写图片描述
System.out.println(resource.getURL());
打印jar:file:/F:/workspace-nyj/Spring_1/kaptcha-2.3.2.jar!/test.properties


记得资源不存在于文件系统中,而是存在jar包中。所以ClassPtahResource不能用getfile(),而应用getURL()

UrlResource

http:通过标准的http协议,访问web资源
ftp:通过标准的ftp协议,访问web资源
file:通过file协议访问本地文件资源系统

UrlResourceTest .java

package com.test;

import java.io.File;
import java.net.MalformedURLException;

import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

public class UrlResourceTest {
    @Test
    public void  UrlResourceTest(){
        Resource resource;
        try {
            resource = new UrlResource("file:d:/a.txt");
            if(resource.exists()){
                ResourceUtil.stream(resource);      
            }
            resource=new UrlResource("http://www.baidu.com");
            if(resource.exists()){
                ResourceUtil.stream(resource);      
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值