springBoot加载resources下静态文件

一、如图,想要加载解析 resources下的 test.txt

 

 

 二、方式一(获取流或者路径)

//获取文件
final File file = ResourceUtils.getFile("classpath:static/stop_words.txt");
//获取文件输入流
final FileInputStream inputStream = new FileInputStream(file);
//获取文件路径
final String path = ResourceUtils.getFile("classpath:static/stop_words.txt").getPath();

三、方式二

   ClassPathResource classPathResource = new ClassPathResource("static/test.txt");
   //获取流
  InputStream inputStream = classPathResource.getInputStream();
  //获取文件
  final File file = classPathResource.getFile();
  //获取路径
  final String path = classPathResource.getPath();

 

posted @ 2021-11-26 15:55  Dragon_Smile  阅读(882)  评论(0编辑  收藏  举报