SpringBoot读取 Resource下文件

@bruce  August 20, 2021
SpringBoot 项目在打包成jar文件后运行在服务器上后是无法读取到 jar 文件中的内容的,读取只能通过类加载器读取
  • 方法一
ClassPathResource classPathResource = new ClassPathResource("templates/index.html");
InputStream inputStream = classPathResource.getInputStream();
  • 方法二
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("templates/index.html");
  • 方法三
InputStream inputStream  = this.getClass().getResourceAsStream("templates/index.html");

添加新评论