java路径和servlet路径和flex路径的获取

来源:互联网 发布:sql字符串拼接赋值 编辑:程序博客网 时间:2024/05/02 01:40

1.servlet中获取路径

Java代码  收藏代码
  1. System.out.println("服务器中的路径:"+request.getContextPath());  
  2. //服务器中的路径:/proj_valuation  
  3. System.out.println("路径:"+request.getServletPath());  
  4. //路径:/FileUploadServlet  
  5. System.out.println("绝对路径:"+request.getSession().getServletContext().getRealPath("/"));  
  6. //绝对路径:E:\apache-tomcat-6.0.26\webapps\proj_valuation\  
  7. String pathStr=request.getSession().getServletContext().getRealPath("/");  
  8. if(!pathStr.isEmpty())  
  9. {  
  10.  pathStr = pathStr.replace('\\', '/');  
  11. }  

 

2.java中获取路径

Java代码  收藏代码
  1. String path1 =getClass().getProtectionDomain().getCodeSource().getLocation().getPath();  
  2. //路径1/E:/apache-tomcat-6.0.26/webapps/proj_valuation/WEB-INF/lib/cglib-2.1.3.jar  
  3. String path2 =this.getClass().getClassLoader().getResource("/").getPath();  
  4. //classes路径1/E:/apache-tomcat-6.0.26/webapps/proj_valuation/WEB-INF/classes/  
  5. String path3 = FlexContext.getHttpRequest().getContextPath();  
  6. //blazeDS获取到的ContextPath路径/proj_valuation  
  7. String path4 = FlexContext.getHttpRequest().getSession().getServletContext().getRealPath("/");  
  8. //blazeDS获取到的RealPath路径E:\apache-tomcat-6.0.26\webapps\proj_valuation\  
  9.   
  10. System.out.println("路径1"+path1);  
  11. System.out.println("classes路径1"+path2);  
  12. System.out.println("blazeDS获取到的ContextPath路径"+path3);  
  13. System.out.println("blazeDS获取到的RealPath路径"+path4);  
  14.   
  15. if(!path4.isEmpty())  
  16.   
  17.  path4 = path4.replace('\\', '/');  

 

3.网上的一些总结

 

Java代码  收藏代码
  1. 1.可以在servlet的init方法里  
  2. String path = getServletContext().getRealPath("/");  
  3. 这将获取web项目的全路径  
  4. 例如 :E:\eclipseM9\workspace\tree\  
  5. tree是我web项目的根目录  
  6.   
  7. 2.你也可以随时在任意的class里调用  
  8. this.getClass().getClassLoader().getResource("/").getPath();  
  9. 这将获取 到classes目录的全路径  
  10. 例如 : E:\eclipseM9/workspace/tree/WEB-INF/classes/  
  11.   
  12. 这个方法也可以不在web环境里确定路径,比较好用  
  13.   
  14. 3.request.getContextPath();  
  15. 获得web根的上下文环境  
  16. 如 /tree  
  17. tree是我的web项目的root context  
  18.   
  19. /*jsp 取得当前目录的路径  
  20. path=request.getRealPath("");  
  21. /*得到jbossWEB发布临时目录 warUrl=.../tmp/deploy/tmp14544test-exp.war/  
  22. path=C:\jboss-4.0.5.GA\server\default\tmp\deploy\tmp14544test-exp.war\  
  23.   
  24. String   path   =   (String)request.getContextPath();  
  25. /*得到项目(test)应用所在的真实的路径 path=/test   
  26. String     path     =   request.getRequestURI();  
  27. /*得到应用所在的真实的路径 path=/test/admin/admindex.jsp  
  28.   
  29. String   savePath=request.getRealPath(request.getServletPath());  
  30. /*得到当前文件的磁盘绝对路径  
  31.   
  32. //JAVA 取得当前目录的路径  
  33. File   file=new   File(".");     
  34. String path=file.getAbsolutePath();  
  35.                 path=file.getPath();  
  36. /*得到jboss运行目录 path=C:\jboss-4.0.5.GA\bin\  

 

 

4.一些有用或者没有的资料

Java代码  收藏代码
  1. web工程:   
  2. servletContext.getRealPath("/");(当前web应用的绝对路径)   
  3. 根目录所对应的绝对路径:request.getServletPath();   
  4. 普通的java:   
  5. 单独的Java类中获得绝对路径:   
  6. FileTest.class.getClassLoader().getResource("")   

 

JSP/Servlet中获得当前web应用程序的相对路径和绝对路径:http://blogold.chinaunix.net/u2/65993/showart_574062.html

0 0
原创粉丝点击