java取得web工程的目录

1.可以在servlet的init方法里
      String path=getServletContext().getRealPath("/");

    //这将获取web项目的路径 例如 :E:\eclipseM9\workspace\tree\ 。ree是我web项目的根目录

2  可以随时再任意的class中调用
     this.getClass().getClassLoader().getResource("/").getPath()

  这获取到classes目录的全路径
   例如 : E:\eclipseM9/workspace/tree/WEB-INF/classes/

   这个方法也可以不在web环境里确定路径,比较好用

request.getContextPath()

 

   获取web根的上下文环境,如 /tree

   tree是我的web项目的root context

4 *jsp 取得当前目录的路径

   path=request.getRealPath("");

 

 *得到jbossWEB发布临时目录 warUrl=.../tmp/deploy/tmp14544test-exp.war/
  path=C:\jboss-4.0.5.GA\server\default\tmp\deploy\tmp14544test-exp.war\

5  String path = (String)request.getContextPath();

  /*得到项目(test)应用所在的真实的路径 path=/test
   String path = request.getRequestURI();
 /*得到应用所在的真实的路径 path=/test/admin/admindex.jsp

6 String savePath=request.getRealPath(request.getServletPath());

   /*得到当前文件的磁盘绝对路径 

7 //JAVA 取得当前目录的路径

   file=new File(".");
   String path=file.getAbsolutePath();
   path=file.getPath();

 /*得到jboss运行目录 path=C:\jboss-4.0.5.GA\bin\

猜你喜欢

转载自huzhiyong56.iteye.com/blog/1290572