SpringBoot-读取classpath下文件

文章目录

  开发过程中,必不可少的需要读取文件,对于打包方式的不同,还会存在一些坑,比如以jar包方式部署时,文件都存在于jar包中,某些读取方式在开发工程中都可行,但是打包后,由于文件被保存在jar中,会导致读取失败。
  这时就需要通过类加载器读取文件,类加载器可以读取jar包中的class类当然也可以读取jar包中的文件。

// 方法1:获取文件或流
this.getClass().getResource("/")+fileName;
this.getClass().getResourceAsStream(failName);
// 方法2:获取文件
File file = org.springframework.util.ResourceUtils.getFile("classpath:test.txt");
// 方法3:获取文件或流
ClassPathResource classPathResource = new ClassPathResource("test.txt");
classPathResource .getFile();
classPathResource .getInputStream();

// >>>>>>>>>>>>>>>> 下面方法可以读取jar包下文件
假设resources目录下有一个test.txt文件,首先获得当前的类加载器,通过类加载器读取文件。

// 方法1
InputStream io = Thread.currentThread().getContextClassLoader().getResourceAsStream("test.txt");
// 方法2
InputStream io = getClass().getClassLoader().getResourceAsStream("test.txt");

 


注意:Spring工具类会对classpath路径做处理,类加载器不会对classpath做处理,因此使用类加载器读取文件,路径中不要添加classpath

posted @ 2019-07-03 11:52  lywJee  阅读(7771)  评论(0编辑  收藏  举报