得到classpath和当前类的绝对路径的一些方法

得到classpath和当前类的绝对路径的一些方法

  下面是一些得到classpath和当前类的绝对路径的一些方法。你可能需要使用其中的一些方法来得到你需要的资源的绝对路径。

  1.FileTest.class.getResource("")

  得到的是当前类FileTest.class文件的URI目录。不包括自己!

  如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/com/test/

  2.FileTest.class.getResource("/")

  得到的是当前的classpath的绝对URI路径。

  如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/

  3.Thread.currentThread().getContextClassLoader().getResource("")

  得到的也是当前ClassPath的绝对URI路径。

  如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/

  4.FileTest.class.getClassLoader().getResource("")

  得到的也是当前ClassPath的绝对URI路径。

  如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/

  5.ClassLoader.getSystemResource("")

  得到的也是当前ClassPath的绝对URI路径。

  如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/

  我推荐使用Thread.currentThread().getContextClassLoader().getResource("")来得到当前的classpath的绝对路径的URI表示法。

  Web应用程序中资源的寻址

  上文中说过,当前用户目录,即相对于System.getProperty("user.dir")返回的路径。

  对于JavaEE服务器,这可能是服务器的某个路径,这个并没有统一的规范!

  而不是我们发布的Web应用程序的根目录!

  这样,在Web应用程序中,我们绝对不能使用相对于当前用户目录的相对路径。

  在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。

  这样,我们只需要提供相对于Web应用程序根目录的路径,就可以构建出定位资源的绝对路径。

  这是我们开发Web应用程序时一般所采取的策略。

 

 

 

 

jsp中的绝对路径和相对路径(转)
2009-05-31 22:26

1.基本概念的理解

  绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:
C:\xyz\test.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm也代表了一个
URL绝对路径。

  相对路径:相对与某个基准目录的路径。包含Web的相对路径(HTML中的相对目录),例如:在Servlet中,"/"代表Web应用的跟目录。和物理路径的相对表示。例如:"./" 代表当前目录,"../"代表上级目录。这种类似的表示,也是属于相对路径。

另外关于URI,URL,URN等内容,请参考RFC相关文档标准。

RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax,
(http://www.ietf.org/rfc/rfc2396.txt)


2.关于JSP/Servlet中的相对路径和绝对路径。

2.1 服务器端的地址

    服务器端的相对地址指的是相对于你的web应用的地址,这个地址是在服务器端解析的(不同于html和javascript中的相对地址,他们是由客户端 浏览器解析的)也就是说这时候在jsp和servlet中的相对地址应该是相对于你的web应用,即相对于http: //192.168.0.1/webapp/的。

  其用到的地方有: forward:servlet中的request.getRequestDispatcher(address); 这个address是在服务器端解析的,所以,你要forward到a.jsp应该这么写:request.getRequestDispatcher (“/user/a.jsp”)这个/相对于当前的web应用webapp,其绝对地址就是:http: //192.168.0.1/webapp/user/a.jsp。
sendRedirect:在jsp中<%response.sendRedirect("/rtccp/user/a.jsp"); %>

2.22 客户端的地址

所有的html页面中的相对地址都是相对于服务器根目录http://192.168.0.1/)的,而不是(跟目录下的该Web应用的目录)http: //192.168.0.1/webapp/的。 Html中的form表单的action属性的地址应该是相对于服务器根目录(http://192.168.0.1/)的,所以,如果提交到a.jsp 为:action="/webapp/user/a.jsp"或action="<%=request.getContextPath()% >"/user/a.jsp;
  提交到servlet为actiom="/webapp/handleservlet"
  Javascript也是在客户端解析的,所以其相对路径和form表单一样。

   因此,一般情况下,在JSP/HTML页面等引用的CSS,Javascript.Action等属性前面最好都加上<%= request.getContextPath()%>,以确保所引用的文件都属于Web应用中的目录。另外,应该尽量避免使用类似".", "./","../../"等类似的相对该文件位置的相对路径,这样当文件移动时,很容易出问题。

3. JSP/Servlet中获得当前应用的相对路径和绝对路径

3.1 JSP中获得当前应用的相对路径和绝对路径

Java代码
  1. 根目录所对应的绝对路径:request.getRequestURI()   
  2. 文件的绝对路径  :application.getRealPath(request.getRequestURI());   
  3. 当前web应用的绝对路径 :application.getRealPath("/");   
  4. 取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()  

3.2 Servlet中获得当前应用的相对路径和绝对路径
       根目录所对应的绝对路径:request.getServletPath();
      文件的绝对路径 :request.getSession().getServletContext().getRealPath (request.getRequestURI())
      当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");

    ServletContext对象获得几种方式:

   Java代码
  1. javax.servlet.http.HttpSession.getServletContext()   
  2. javax.servlet.jsp.PageContext.getServletContext()   
  3. javax.servlet.ServletConfig.getServletContext()   

4.java 的Class中获得相对路径,绝对路径的方法

4.1 单独的Java类中获得绝对路径
  根据java.io.File的Doc文挡,可知:
       默认情况下new File("/")代表的目录为:System.getProperty("user.dir")。
       一下程序获得执行类的当前路径

      Java代码
  1. package org.cheng.file;   
  2. import java.io.File;   
  3.   
  4. public class FileTest {   
  5. public static void main(String[] args) throws Exception {   
  6.   
  7. System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));   
  8.   
  9. System.out.println(FileTest.class.getClassLoader().getResource(""));   
  10.   
  11. System.out.println(ClassLoader.getSystemResource(""));   
  12. System.out.println(FileTest.class.getResource(""));   
  13. System.out.println(FileTest.class.getResource("/")); //Class文件所在路径   
  14. System.out.println(new File("/").getAbsolutePath());   
  15. System.out.println(System.getProperty("user.dir"));   
  16. }   
  17. }  


4.2服务器中的Java类获得当前路径(来自网络)

(1).Weblogic

WebApplication的系统文件根目录是你的weblogic安装所在根目录。
例如:如果你的weblogic安装在c:\bea\weblogic700.....
那么,你的文件根路径就是c:\.
所以,有两种方式能够让你访问你的服务器端的文件:
a.使用绝对路径:
比如将你的参数文件放在c:\yourconfig\yourconf.properties,
直接使用 new FileInputStream("yourconfig/yourconf.properties");
b.使用相对路径:
相对路径的根目录就是你的webapplication的根路径,即WEB-INF的上一级目录,将你的参数文件放在yourwebapp\yourconfig\yourconf.properties,
这样使用:new FileInputStream("./yourconfig/yourconf.properties");
这两种方式均可,自己选择。

(2).Tomcat

在类中输出System.getProperty("user.dir"); 显示的是%Tomcat_Home%/bin

(3).Resin

不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET的路径为根.比如用新建文件法测试File f = new File("a.htm"); 这个a.htm在resin的安装目录下

(4).如何读相对路径哪?

在Java文件中getResource或getResourceAsStream均可

例:getClass().getResourceAsStream(filePath); //filePath可以是"/filename",这里的/代表web

发布根路径下WEB-INF/classes

默认使用该方法的路径是:WEB-INF/classes。已经在Tomcat中测试。

5.读取文件时的相对路径,避免硬编码和绝对路径的使用。(来自网络)

5.1 采用Spring的DI机制获得文件,避免硬编码。
参考下面的连接内容:
http://www.javajia.net/viewtopic.php?p=90213&
5.2 配置文件的读取
参考下面的连接内容:
http://dev.youkuaiyun.com/develop/article/39/39681.shtm
5.3 通过虚拟路径或相对路径读取一个xml文件,避免硬编码
参考下面的连接内容:
http://club.gamvan.com/club/clubPage.jsp?iPage=1&tID=10708&ccID=8

6.Java中文件的常用操作(复制,移动,删除,创建等)(来自网络)
常用 java File 操作类
http://www.easydone.cn/014/200604022353065155.htm

Java文件操作大全(JSP中)
http://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html

java文件操作详解(Java中文网)
http://www.51cto.com/html/2005/1108/10947.htm

JAVA 如何创建\删除\修改\复制目录及文件
http://www.gamvan.com/developer/java/2005/2/264.html

Java代码

  1. import    java.net.URL;   
  2. /**
  3.    *    Java通用的绝对路径获取终极解决方案
  4.    *    获取classes,WEB-INF等的绝对路径
  5.    *    @测试系统:
  6.    *    Windows    Linux    Unix
  7.    *    @测试环境:
  8.    *    Tomcat6.0.10
  9.    *    Resin3.1.1
  10.    *    Jboss4.0.0/5.0.0
  11.    *    WebLogic9.1
  12.    *    WebSphere6.1
  13.    *    WasCE1.1
  14.    *    Apusic4.0.3
  15.    *    JFox3
  16.    *    Jetty6.1.3
  17.    *   
  18.    *    @author    Jxva
  19.    *    @support    Jxva.Com
  20.    *
  21.    */   
  22. public   class    JPath   
  23. {   
  24. public   static   void    main(String    args[])   
  25. {   
  26. JPath    j=new    JPath();   
  27. System.out.println(getClassPath());   
  28. System.out.println(getWebinfoPath());   
  29. System.out.println(getJxvaHome());   
  30. System.out.println(getAppPath());   
  31. System.out.println(getRootPath());   
  32. System.out.println(getJxvaFramework());   
  33. System.out.println(j.getRealPath());   
  34. }   
  35.   
  36. /**
  37.    *   
  38.    *    @return    ~/classes/
  39.    */   
  40. public   static    String    getClassPath(){   
  41. JPath    j=new    JPath();   
  42. String    t=j.getRealPath();   
  43. t=t.replaceAll( "file:/ ",   " "); //windows   
  44. t=t.replaceAll( "file: ",   " "); //linux,unix   
  45. t=t.replaceAll( "wsjar: ", " "); //websphere    wsjar:    has    to    at    jar:    before   
  46. t=t.replaceAll( "jar: ", " "); //tomcat,jboss,resin,wasce,apusic   
  47. t=t.replaceAll( "zip: ", " "); //weblogic   
  48. t=t.replaceAll( "/./ ", "/ "); //weblogic   
  49. //if    this    class    be    included    .jar    file,will    replace    "/lib/*.!/ "    to    "/classes/ "   
  50. t=t.replaceAll( "/lib/([^\ ' ']+)!/ ", "/classes/ ");   //jar   
  51. t=t.split( "/classes/ ")[0]+ "/classes/ ";   
  52. return    t;   
  53. }   
  54.   
  55. /**   
  56.    *   if   this    path    include   "WEB-INF "    will   return   "WEB-INF " 's    absolute    path   
  57.    *   @return    ~/WEB-INF/   
  58.    */   
  59. public   static    String    getWebinfoPath(){   
  60. //remove    string    "classes/ "   
  61. return    getClassPath().substring(0,getClassPath().length()-8);   
  62. }   
  63.   
  64. /**
  65.    *   
  66.    *    @return    ~/JxvaHome/
  67.    */   
  68. public   static    String    getJxvaHome(){   
  69. return    getWebinfoPath()+ "JxvaHome/ ";   
  70. }   
  71.   
  72. /**
  73.    *   
  74.    *    @return    application 's    path
  75.    */   
  76. public   static    String    getAppPath(){   
  77. String[]    s=getWebinfoPath().split( "/ ");   
  78. StringBuffer    sb=new    StringBuffer();   
  79. for(int    i=0;i <s.length-1;i++){   
  80. sb.append(s[i]+ "/ ");   
  81. }   
  82. return    sb.toString();   
  83. }   
  84.   
  85. /**
  86.    *   
  87.    *    @return    root 's    path
  88.    */   
  89. public   static    String    getRootPath(){   
  90. return    getClassPath().split( "/ ")[0]+ "/ ";   
  91. }   
  92.   
  93. /**
  94.    *    JxvaFramework框架的公共目录,确保对根目录有可写权限,一般虚拟主机将无法使用
  95.    *    @return    JxvaFrameowrk
  96.    */   
  97. public   static    String    getJxvaFramework(){   
  98. return    getRootPath()+ "JxvaFramework/ ";   
  99. }   
  100.   
  101. private    String    getRealPath()   
  102. {   
  103. String    strClassName    =    getClass().getName();   
  104. String    strPackageName    =   " ";   
  105. if    (getClass().getPackage()    !=   null)    strPackageName    =    getClass().getPackage().getName();   
  106. String    strClassFileName    =   " ";   
  107. if    (! " ".equals(strPackageName))   
  108. strClassFileName    =    strClassName.substring(strPackageName.length()    +   1,    strClassName.length());   
  109. else   
  110. strClassFileName    =    strClassName;   
  111. URL    url    =    getClass().getResource(strClassFileName    +   ".class ");   
  112. String    strURL    =    url.toString();   
  113. strURL    =    strURL.replaceAll( "%20 ",   "    ");   
  114. return    strURL;   
  115. }   
  116. }  


总结:
通过上面内容的使用,可以解决在Web应用服务器端,移动文件,查找文件,复制
删除文件等操作,同时对服务器的相对地址,绝对地址概念更加清晰。
建议参考URI,的RFC标准文挡。同时对Java.io.File. Java.net.URI.等内容了解透彻
对其他方面的理解可以更加深入和透彻。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值