首页 技术 正文
技术 2022年11月23日
0 收藏 989 点赞 3,742 浏览 1026 个字

Resource与ResourceLoader对比

1、Resource接口定义了应用访问底层资源的能力。

  • 通过FileSystemResource以文件系统绝对路径的方式进行访问;
  • 通过ClassPathResource以类路径的方式进行访问;
  • 通过ServletContextResource以相对于Web应用根目录的方式进行访问。

  在获取资源后,用户就可以通过Resource接口定义的多个方法访问文件的数据和其他的信息:如可以通过getFileName()获取文件名,通过getFile()获取资源对应的File对象,通过getInputStream()直接获取文件的输入流。此外,还可以通过createRelative(String relativePath)在资源相对地址上创建新的文件。

2、ResourceLoader接口提供了一个加载文件的策略。它提供了一个默认的实现类DefaultResourceLoader,获取资源代码如下:

 @Override
public Resource getResource(String location) {
Assert.notNull(location, "Location must not be null");
if (location.startsWith("/")) {
return getResourceByPath(location);
}
else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
}
else {
try {
// Try to parse the location as a URL...
URL url = new URL(location);
return new UrlResource(url);
}
catch (MalformedURLException ex) {
// No URL -> resolve as resource path.
return getResourceByPath(location);
}
}
}
 protected Resource getResourceByPath(String path) {
return new ClassPathContextResource(path, getClassLoader());
}

微信扫一扫

支付宝扫一扫

本文网址:https://www.zhankr.net/141491.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:7,140
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:4,476
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:567 阅读:5,290
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:5,162
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:6,335
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:3,874