传统的Java资源文件的访问通过JDK中的File、URL类难以满足各种不同需求的资源加载,这里有Spring中设计的Resource接口提供更加强大的访问底层资源的能力。 spring 中定义了资源接口,部分类关系如下: InputStreamSource接口方法:InputStream getInputStream() throws IOException;:返回资源对应的输入流 下面是Resource接口的主要方法: Resource实现类有很多,常见的具体实现类如下: 如下分别通过FileSystemResource和ClassPathResource访问同一个文件资源: 获取到资源后,可以通过getFileName()获取方法名,通过getFile()获取资源对应的File文件,通过getInputStream()直接获取文件的输入流,还可通过createRelative(String relativePath)在资源相对地址上创建新的文件。 如下是在Web应用中通过ServletContextResource以相对于Web应用根目录的方式访问文件资源: resource.jsp 可以通过EncodedResource对资源进行编码,保证资源内容操作的正确性,如下: spring中资源加载的地址前缀如下: 其中,classpath:和classpath:的区别在于classpath:会扫描所有类路径下出现的资源,而classpath只会在第一个加载的包下查找,即就是只加载一个资源文件。 在FileSystemResource 中需要给出spring-config.xml文件在你项目中的相对路径或者绝对路径。在ClassPathResource中spring会在ClassPath中自动搜寻配置文件,所以要把ClassPathResource 文件放在ClassPath下。 如果将spring-config.xml保存在了src文件夹下的话,只需给出配置文件的名称即可,因为src文件夹是默认。 简而言之,ClassPathResource在环境变量中读取配置文件,FileSystemResource在配置文件中读取配置文件。24、FileSystemResource和ClassPathResource有何区别?
一、概述
二、Resource接口
三、Resource接口实现类
四、区别
(1)FileSystemResource和ClassPathResource
(2)ServletContextResource
(3)EncodedResource
五、总结(面试回答)一、概述
二、Resource接口
三、Resource接口实现类
四、区别
(1)FileSystemResource和ClassPathResource
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 = "D:/Spring/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();
}
}
}
(2)ServletContextResource
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<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());
%>
(3)EncodedResource
package com.baobaotao.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);
}
}
五、总结(面试回答)