zoukankan      html  css  js  c++  java
  • 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);
    }

  • 相关阅读:
    Duff and Meat(贪心)
    Duff and Meat(贪心)
    Eugeny and Array(水题,注意题目描述即可)
    Eugeny and Array(水题,注意题目描述即可)
    HDU-2588-GCD (欧拉函数)
    HDU-2588-GCD (欧拉函数)
    再谈欧拉函数
    再谈欧拉函数
    容斥定理及浅略介绍
    Vue
  • 原文地址:https://www.cnblogs.com/lixiaoran/p/13885746.html
Copyright © 2011-2022 走看看