博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring之ResourceLoader加载资源
阅读量:7101 次
发布时间:2019-06-28

本文共 1349 字,大约阅读时间需要 4 分钟。

Resource与ResourceLoader对比

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

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

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

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

1 @Override 2 public Resource getResource(String location) { 3     Assert.notNull(location, "Location must not be null"); 4     if (location.startsWith("/")) { 5         return getResourceByPath(location); 6     } 7     else if (location.startsWith(CLASSPATH_URL_PREFIX)) { 8         return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader()); 9     }10     else {11         try {12             // Try to parse the location as a URL...13             URL url = new URL(location);14             return new UrlResource(url);15         }16         catch (MalformedURLException ex) {17             // No URL -> resolve as resource path.18             return getResourceByPath(location);19         }20     }21 }

 

1 protected Resource getResourceByPath(String path) {2     return new ClassPathContextResource(path, getClassLoader());3 }

 

转载于:https://www.cnblogs.com/luyanliang/p/5566574.html

你可能感兴趣的文章
oracle log_archive_dest_1 未指定导致flash_recovery_area引发数据库挂起
查看>>
性能测试培训笔记-安装loadrunner出现vc2005_sp1_with_atl_fix_redist
查看>>
Puppet函数介绍(十八)
查看>>
数据中心开发者定义
查看>>
iOS网络编程-ASIHTTPRequest框架同步请求
查看>>
马哥Linux大型免费公开课即将开始(马哥多年经验首次对外公开)
查看>>
可穿戴操作系统,期待吗?(二)
查看>>
阿里巴巴赴美上市,市值将超千亿
查看>>
14.Azure流量管理器(下)
查看>>
wordpress 切换语言/语言包
查看>>
艾伟:这下没理由嫌Eval的性能差了吧?
查看>>
Java,net上的几篇文章
查看>>
Chrome的Awesome Screenshot的插件离线下载
查看>>
改变self.navigationItem的显示标题
查看>>
Revit2014机电系统类型BUG
查看>>
android Handler的使用(一)
查看>>
函数指针
查看>>
数学图形之Boy surface
查看>>
linux远程连接
查看>>
最快速的Android开发环境搭建ADT-Bundle及Hello World
查看>>