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

时间:2020-10-27 18:36:26   收藏:0   阅读:169

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);
}

原文:https://www.cnblogs.com/lixiaoran/p/13885746.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!