获取WEB-INF/classes的实际路径的方法

WEB-INF/classes用于存放相关的类以及常用的配置文件,且该目录不会被客户端读取到。
获取其目录的方法示例

public static String getXMLPath(String cfgfile){
    String XMLPath = 
Thread.currentThread().getContextClassLoader().getResource("").toString();
    XMLPath = XMLPath.replace("file:/", "");
    //XMLPath初始为类似于 file:/D:/workdir/.metadata/.plugins/ 这样的格式,需要将file:/去掉。
    XMLPath = XMLPath.replace("%20", " ");
    //XMLPath中如果有空格,getResource会将其变成%20
    //System.out.println(XMLPath);
    return XMLPath + cfgfile;
}

标签: none

添加新评论