当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
Java获得web的根路径
发布时间:2010/7/30 10:17:01 来源:城市学习网 编辑:ziteng
  java获取根路径有两种方式:
  1),在servlet可以用一下方法取得:
  request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/”)+”//upload//”;
  2),不从jsp,或servlet中获取,只从普通java类中获取:
  String path =
  getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
  SAXReader()  saxReader = new SAXReader();
  if(path.indexOf(“WEB-INF”)>0){
  path = path.substring(0,path.indexOf(“/WEB-INF/classes”)+16);
  //  ‘/WEB-INF/classes’为16位
  document = saxReader.read(path+filename);
  }else{
  document = saxReader.read(getClass().getResourceAsStream(filename));
  }
  weblogic   tomcat 下都有效
  String path =
  getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
  <!--EndFragment-->
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved