`
zch198627
  • 浏览: 39304 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

java类获取web应用的根目录(转载)

    博客分类:
  • JAVA
阅读更多
2007年08月08日 星期三 01:51

package cn.gyd.util.test;

/**
*


* java类获取web应用的根目录
*


*/
public class PathUtil {

/**
   * @param args
   */
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;
}
}

分享到:
评论
1 楼 yuanyuan7891 2010-08-25  
有没有jsp版本的啊

相关推荐

Global site tag (gtag.js) - Google Analytics