设为首页 加入收藏

TOP

Java获得web的根路径
2014-10-19 16:11:18 来源: 作者: 【 】 浏览:94
Tags:Java 获得 web 路径

  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();


  


  编辑特别推荐:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇直接用java命令行动态生成jpg文件 下一篇在Java中通过SQL标记完成数据库的..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: