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

 

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);		
	}
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值