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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM