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、一些实例:
- <span style="font-family:Microsoft YaHei; font-size:14px">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();
- }
- }
- }</span>
- <span style="font-family:Microsoft YaHei; font-size:14px"><%@ 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></span>
对于远程服务器的文件系统,用户可以方便地通过UrlResource进行访问。
4、文件编码问题
资源加载时默认采用系统编码读取资源内容,如果资源文件采用特殊的编码格式,那么可以通过EncodedResource对资源进行编码,以保证资源内容的正确性。
- <span style="font-family:Microsoft YaHei; font-size:14px">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);
- }
- }</span><strong style="font-size:18px">
- </strong>