public static String getWebRootPath(Class<?> clazz) { String webClassesPath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath(); if (webClassesPath.indexOf("WEB-INF") > 0) { String WebRootpath = webClassesPath.substring(0, webClassesPath.indexOf("WEB-INF/classes")); return WebRootpath; } else { return null; } } |
调用这个方法的方式
getWebRootPath(HandleController.getClass()); |