Springboot resource下文件(文件夹)读取

SpringBoot打包后无法访问JAR中的路径,所以必须使用resource.getInputStream(),

直接读取文件异常如下:

java.io.FileNotFoundException: class path resource [validator-config/battery.xml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/idea_workspace/SVN/mx_vehicle_parts_management/branches/mx_vehicle_parts_management_springboot/mx-vehicle-parts-management-web/target/mx-vehicle-parts-management-web-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes!/validator-config/battery.xml

 

resource下文件读取:

  1.  
    ClassPathResource resource = new ClassPathResource("validator-config/battery.xml");
  2.  
    InputStream inputStream = resource.getInputStream();
  3.  
    String xmlContent = IOUtils.toString(inputStream, "UTF-8");

resource下文件夹读取:

Resource[] resources = new PathMatchingResourcePatternResolver().getResources("validator-config/*.xml");
for (int i = 0; i < resources.length; i++) {
    InputStream inputStream = resources[i].getInputStream();
    String xmlContent = IOUtils.toString(inputStream, "UTF-8");
    System.out.println("content" + i + "=" + xmlContent);
}

posted @ 2020-10-27 16:46  然然1907  阅读(2964)  评论(0编辑  收藏  举报