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下文件读取:

ClassPathResource resource = new ClassPathResource("validator-config/battery.xml");
InputStream inputStream = resource.getInputStream();
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);
}

 

你可能感兴趣的:(springboot)