获取项目物理根目录绝对路径

//获取项目物理根目录绝对路径
    private String getWebAppRootPath(){
        String result = TimerTask.class.getResource("TimerTask.class").toString();
        int index = result.indexOf("WEB-INF");
        
        result = result.substring(0, index);
        if (result.startsWith("zip")) { // 当class文件在war中时,返回"zip:D:/ ..."样的路径
            result = result.substring(4);
        }
        else if (result.startsWith("file")) { // 当class文件在class文件中时,返回"file:/F:/ ..."样的路径
            result = result.substring(6);
        }else if(result.startsWith("jar")){ // 当class文件在jar文件中时,返回"jar:file:/F:/ ..."样的路径
            result = result.substring(10);
        }
        if (result.endsWith("/")) result = result.substring(0, result.length() - 1);// 不包含最后的"/"
            result = result.replace("%20", " "); //Window下替换空格
            return result;
        }

//windows下
if("\\".equals(File.separator)){
template = template.replace("/", "\\");
path = path.replace("/", "\\");
}
//linux下
if("/".equals(File.separator)){
template = template.replace("\\", "/");
path = path.replace("\\", "/");
}

原文地址:https://www.cnblogs.com/zengdingxin/p/3133582.html