aloe
public class PathUtil {


public static void main(String[] args)throws Exception {
   PathUtil p = new PathUtil();
   System.out.println(p.getWebClassesPath());
   System.out.println(p.getWebInfPath());
   System.out.println(p.getWebRoot());
}

public String getWebClassesPath() {
   String path = getClass().getProtectionDomain().getCodeSource()
     .getLocation().getPath();
   return path;
  
}

public String getWebInfPath() throws IllegalAccessException{
   String path = getWebClassesPath();
   if (path.indexOf("WEB-INF") > 0) {
    path = path.substring(0, path.indexOf("WEB-INF")+8);
   } else {
    throw new IllegalAccessException("路径获取错误");
   }
   return path;
}

public String getWebRoot() throws IllegalAccessException{
   String path = getWebClassesPath();
   if (path.indexOf("WEB-INF") > 0) {
    path = path.substring(0, path.indexOf("WEB-INF/classes"));
   } else {
    throw new IllegalAccessException("路径获取错误");
   }
   return path;
}
} 

分类:

技术点:

相关文章:

  • 2021-11-27
  • 2021-11-27
  • 2021-11-23
  • 2021-12-26
  • 2021-11-28
  • 2021-11-30
  • 2021-11-18
猜你喜欢
  • 2021-11-19
  • 2021-11-19
  • 2021-11-29
  • 2021-09-01
  • 2021-11-23
  • 2021-11-23
  • 2021-10-17
相关资源
相似解决方案