得到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中获得当前应用的相对路径和绝对路径
- 根目录所对应的绝对路径:request.getRequestURI()
- 文件的绝对路径 :application.getRealPath(request.getRequestURI());
- 当前web应用的绝对路径 :application.getRealPath("/");
- 取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()
3.2 Servlet中获得当前应用的相对路径和绝对路径 根目录所对应的绝对路径:request.getServletPath(); 文件的绝对路径 :request.getSession().getServletContext().getRealPath (request.getRequestURI()) 当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");
ServletContext对象获得几种方式:
- javax.servlet.http.HttpSession.getServletContext()
- javax.servlet.jsp.PageContext.getServletContext()
- javax.servlet.ServletConfig.getServletContext()
4.java 的Class中获得相对路径,绝对路径的方法
4.1 单独的Java类中获得绝对路径 根据java.io.File的Doc文挡,可知: 默认情况下new File("/")代表的目录为:System.getProperty("user.dir")。 一下程序获得执行类的当前路径
- package org.cheng.file;
- import java.io.File;
-
- public class FileTest {
- public static void main(String[] args) throws Exception {
-
- System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));
-
- System.out.println(FileTest.class.getClassLoader().getResource(""));
-
- System.out.println(ClassLoader.getSystemResource(""));
- System.out.println(FileTest.class.getResource(""));
- System.out.println(FileTest.class.getResource("/"));
- System.out.println(new File("/").getAbsolutePath());
- System.out.println(System.getProperty("user.dir"));
- }
- }
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代码
- import java.net.URL;
-
- public class JPath
- {
- public static void main(String args[])
- {
- JPath j=new JPath();
- System.out.println(getClassPath());
- System.out.println(getWebinfoPath());
- System.out.println(getJxvaHome());
- System.out.println(getAppPath());
- System.out.println(getRootPath());
- System.out.println(getJxvaFramework());
- System.out.println(j.getRealPath());
- }
-
-
- public static String getClassPath(){
- JPath j=new JPath();
- String t=j.getRealPath();
- t=t.replaceAll( "file:/ ", " ");
- t=t.replaceAll( "file: ", " ");
- t=t.replaceAll( "wsjar: ", " ");
- t=t.replaceAll( "jar: ", " ");
- t=t.replaceAll( "zip: ", " ");
- t=t.replaceAll( "/./ ", "/ ");
-
- t=t.replaceAll( "/lib/([^\ ' ']+)!/ ", "/classes/ ");
- t=t.split( "/classes/ ")[0]+ "/classes/ ";
- return t;
- }
-
- /**
- * if this path include "WEB-INF " will return "WEB-INF " 's absolute path
- * @return ~/WEB-INF/
- */
- public static String getWebinfoPath(){
-
- return getClassPath().substring(0,getClassPath().length()-8);
- }
-
-
- public static String getJxvaHome(){
- return getWebinfoPath()+ "JxvaHome/ ";
- }
-
-
- public static String getAppPath(){
- String[] s=getWebinfoPath().split( "/ ");
- StringBuffer sb=new StringBuffer();
- for(int i=0;i <s.length-1;i++){
- sb.append(s[i]+ "/ ");
- }
- return sb.toString();
- }
-
-
- public static String getRootPath(){
- return getClassPath().split( "/ ")[0]+ "/ ";
- }
-
-
- public static String getJxvaFramework(){
- return getRootPath()+ "JxvaFramework/ ";
- }
-
- private String getRealPath()
- {
- String strClassName = getClass().getName();
- String strPackageName = " ";
- if (getClass().getPackage() != null) strPackageName = getClass().getPackage().getName();
- String strClassFileName = " ";
- if (! " ".equals(strPackageName))
- strClassFileName = strClassName.substring(strPackageName.length() + 1, strClassName.length());
- else
- strClassFileName = strClassName;
- URL url = getClass().getResource(strClassFileName + ".class ");
- String strURL = url.toString();
- strURL = strURL.replaceAll( "%20 ", " ");
- return strURL;
- }
- }
总结: 通过上面内容的使用,可以解决在Web应用服务器端,移动文件,查找文件,复制 删除文件等操作,同时对服务器的相对地址,绝对地址概念更加清晰。 建议参考URI,的RFC标准文挡。同时对Java.io.File. Java.net.URI.等内容了解透彻 对其他方面的理解可以更加深入和透彻。
|