1. Spring 读取资源
    1. 主要介绍3种方式(当然不止三种,但是这三种基本能应付大多需求)
      FileSystemResource:以文件的绝对路径方式进行访问
      ClassPathResourcee:以类路径的方式访问
      ServletContextResource:web应用根目录的方式访问
    2. 主要公用方法介绍(Resource接口下的)
      getFilename() : 获得文件名称
      contentLength() : 获得文件大小
      createRelative(path) : 在资源的相对地址上创建新文件
      exists() : 是否存在
      getFile() : 获得Java提供的File 对象
      getInputStream() :  获得文件的流
    3. 与常规的对应方法
      FileSystemResource 效果类似于Java中的File
      ClassPathResource 效果类似于this.getClass().getResource("/").getPath();
      ServletContextResource 效果类似于request.getServletContext().getRealPath("");

场景:

读取Resource下export文件夹中的xml配置文件

1.ClassPathResource

    Resource resource = new ClassPathResource("export/config.xml");
File file = resource.getFile(); System.out.println("对应的以往的实现方式:"+this.getClass().getResource("/").getPath());

2.FileSystemResource

     FileSystemResource res1=new FileSystemResource("D:/abc.txt");
File f = res1.getFile(); //转换成Java的File对象

ClassPathResource类的注释:

 * Supports resolution as {@code java.io.File} if the class path
* resource resides in the file system, but not for resources in a JAR.
* Always supports resolution as URL.

Spring 读取资源文件后如果使用getFile()方法获取的话要保证资源文件是在文件系统中(能正确读取出文件路径)

如果运行的Jar包读出的路径是:app.jar!/BOOT-INF!/classes/.....这个样子

最新文章

  1. 使用ASP.NET MVC局部视图避免JS拼接HTML,编写易于维护的HTML页面
  2. Junit基础整理
  3. D:Balanced Lineup
  4. 记录一个bug -- sprintf
  5. POJ2222+暴力搜索
  6. HTTP状态码搜集
  7. SQL函数介绍
  8. jQuery.sort对DOM元素进行排序
  9. 老李分享:接电话之uiautomator 1
  10. Linux配置LNMP环境(一)配置Nginx
  11. c#实现服务端webSocket
  12. BZOJ 1119: [POI2009]SLO [置换群]
  13. js定义数组的方法
  14. 走进Java Map家族 (1) - HashMap实现原理分析
  15. windows许可证即将过期
  16. ☆ [POI2007] ZAP-Queries 「莫比乌斯反演」
  17. linux 内核代码结构
  18. 中位数&贪心
  19. Ice简介+Qt代码示例
  20. Docker技术入门与实战 第二版-学习笔记-3-Dockerfile 指令详解

热门文章

  1. MySQL8.0 redo日志系统优化
  2. CSPS模拟测试59
  3. 原生JS实现栈结构
  4. JavaScrip 基础
  5. CAS3.5.2 Server登录后返回用户信息详细解决方案
  6. HashMap深入分析及使用要点
  7. 2019CSP游记
  8. python3 控制安卓手机的飞行模式遇到的问题汇总
  9. Tsx写一个通用的button组件
  10. Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源