<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A18制造 &#187; 路径</title>
	<atom:link href="http://a18zhizao.com/ytag/%e8%b7%af%e5%be%84/feed" rel="self" type="application/rss+xml" />
	<link>http://a18zhizao.com</link>
	<description>梦想的种子飞入了大地，所需要的只有时间。</description>
	<lastBuildDate>Mon, 26 Jul 2010 04:08:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Java关于路径获取的问题</title>
		<link>http://a18zhizao.com/y2010/1307_java-on-the-path-to-get-the-issue.html</link>
		<comments>http://a18zhizao.com/y2010/1307_java-on-the-path-to-get-the-issue.html#comments</comments>
		<pubDate>Fri, 05 Mar 2010 07:35:58 +0000</pubDate>
		<dc:creator>a18ccms</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[路径]]></category>

		<guid isPermaLink="false">http://a18zhizao.com/?p=1307</guid>
		<description><![CDATA[痛苦的一下午。今天把我做的系统进行移植。不过出毛病了。由于不在IDE下。我也不知道出错信息是什么。这时候才发现LOG是多么重要的了。马上补加LOG类。可是到生成文件的地方卡住了。如何才能在网站的根目录LOGS下生成日志呢。求助G大神。在这里总结下（毛病找到了。忘记装ORACLE的DBA驱动了。。汗） 关于路径问题的API 第一 JSP的路径问题 1.1 JSP中获得当前应用的相对路径和绝对路径 　　根目录所对应的绝对路径:request.getRequestURI() 　　文件的绝对路径 　:application.getRealPath(request.getRequestURI()); 　　当前web应用的绝对路径 :application.getRealPath("/"); 　　取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent() 1.2 Servlet中获得当前应用的相对路径和绝对路径 　　根目录所对应的绝对路径:request.getServletPath(); 　　文件的绝对路径 :request.getSession().getServletContext().getRealPath(request.getRequestURI()) 　　当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/"); 1.3 ServletContext对象获得几种方式：javax.servlet.http.HttpSession.getServletContext() 　　javax.servlet.jsp.PageContext.getServletContext() 　　javax.servlet.ServletConfig.getServletContext() 第二 java 的Class中获得相对路径，绝对路径的方法 2.1单独的Java类中获得绝对路径 　　根据java.io.File的Doc文挡，可知: 　　默认情况下new File("/")代表的目录为：System.getProperty("user.dir")。 　　以下程序获得执行类的当前路径 　　 package org.zhizao.file; 　　import java.io.File; 　　public class FileTest { 　　public static void main(String[] args) throws Exception { 　　System.out.println(Thread.currentThread().getContextClassLoader().getResource(&#34;&#34;)); 　　System.out.println(FileTest.class.getClassLoader().getResource(&#34;&#34;)); 　　System.out.println(ClassLoader.getSystemResource(&#34;&#34;)); 　　System.out.println(FileTest.class.getResource(&#34;&#34;)); 　　System.out.println(FileTest.class.getResource(&#34;/&#34;)); //Class文件所在路径 [...]]]></description>
			<content:encoded><![CDATA[<p>   痛苦的一下午。今天把我做的系统进行移植。不过出毛病了。由于不在IDE下。我也不知道出错信息是什么。这时候才发现LOG是多么重要的了。马上补加LOG类。可是到生成文件的地方卡住了。如何才能在网站的根目录LOGS下生成日志呢。求助G大神。在这里总结下（毛病找到了。忘记装ORACLE的DBA驱动了。。汗）<br />
关于路径问题的API<br />
<strong>第一 JSP的路径问题</strong><br />
1.1 JSP中获得当前应用的相对路径和绝对路径<br />
　　根目录所对应的绝对路径:request.getRequestURI()<br />
　　文件的绝对路径 　:application.getRealPath(request.getRequestURI());<br />
　　当前web应用的绝对路径 :application.getRealPath("/");<br />
　　取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()<br />
1.2 Servlet中获得当前应用的相对路径和绝对路径<br />
　　根目录所对应的绝对路径:request.getServletPath();<br />
　　文件的绝对路径 :request.getSession().getServletContext().getRealPath(request.getRequestURI())<br />
　　当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");<br />
1.3 ServletContext对象获得几种方式：javax.servlet.http.HttpSession.getServletContext()<br />
　　javax.servlet.jsp.PageContext.getServletContext()<br />
　　javax.servlet.ServletConfig.getServletContext()<br />
<span id="more-1307"></span><br />
<strong>第二 java 的Class中获得相对路径，绝对路径的方法</strong><br />
2.1单独的Java类中获得绝对路径<br />
　　根据java.io.File的Doc文挡，可知:<br />
　　默认情况下new File("/")代表的目录为：System.getProperty("user.dir")。<br />
　　以下程序获得执行类的当前路径</p>
<pre class="brush: java;"> 　　
package org.zhizao.file;
　　import java.io.File;
　　public class FileTest {
　　public static void main(String[] args) throws Exception {
　　System.out.println(Thread.currentThread().getContextClassLoader().getResource(&quot;&quot;));
　　System.out.println(FileTest.class.getClassLoader().getResource(&quot;&quot;));
　　System.out.println(ClassLoader.getSystemResource(&quot;&quot;));
　　System.out.println(FileTest.class.getResource(&quot;&quot;));
　　System.out.println(FileTest.class.getResource(&quot;/&quot;)); //Class文件所在路径
　　System.out.println(new File(&quot;/&quot;).getAbsolutePath());
　　System.out.println(System.getProperty(&quot;user.dir&quot;));
　　}
}
</pre>
<p>2.2服务器中的Java类获得当前路径<br />
　　(1).Weblogic<br />
　　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");这两种方式均可，自己选择。<br />
　　(2).Tomcat<br />
　　在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin<br />
　　(3).Resin<br />
　　不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET的路径为根.比如用新建文件法测试File f = new File("a.htm");这个a.htm在resin的安装目录下<br />
　　(4).如何读相对路径哪?<br />
　　在Java文件中getResource或getResourceAsStream均可<br />
　　例：getClass().getResourceAsStream(filePath);//filePath可以是" /filename",这里的/代表web发布根路径下WEB-INF/classes默认使用该方法的路径是：WEB-INF/classes。已经在 Tomcat中测试。<br />
　　总结：<br />
　　通过上面内容的使用，可以解决在Web应用服务器端，移动文件，查找文件，复制删除文件等操作，同时对服务器的相对地址，绝对地址概念更加清晰。建议参考URI,的RFC标准文挡。同时对Java.io.File. Java.net.URI.等内容了解透彻对其他方面的理解可以更加深入和透彻。</p>
<p>关于路径问题的类<br />
一种方式：要创建一个新对象才能获取</p>
<pre class="brush: java;">
package org.zhizao.test;
/**
*
* java类获取web应用的根目录
*
*/
public class PathUtil {

/**
   * @param args
   */
public static void main(String[] args)throws Exception {
   PathUtil p = new PathUtil();
   System.out.println(p.getWebClassesPath());
   System.out.println(p.getWebInfPath());
   System.out.println(p.getWebRoot());
}

public String getWebClassesPath() {
   String path = getClass().getProtectionDomain().getCodeSource()
     .getLocation().getPath();
   return path;

}

public String getWebInfPath() throws IllegalAccessException{
   String path = getWebClassesPath();
   if (path.indexOf(&quot;WEB-INF&quot;) &gt; 0) {
    path = path.substring(0, path.indexOf(&quot;WEB-INF&quot;)+8);
   } else {
    throw new IllegalAccessException(&quot;路径获取错误&quot;);
   }
   return path;
}

public String getWebRoot() throws IllegalAccessException{
   String path = getWebClassesPath();
   if (path.indexOf(&quot;WEB-INF&quot;) &gt; 0) {
    path = path.substring(0, path.indexOf(&quot;WEB-INF/classes&quot;));
   } else {
    throw new IllegalAccessException(&quot;路径获取错误&quot;);
   }
   return path;
}
}
</pre>
<p>第二种方式：写在在静态方法里，无需new一个对象</p>
<pre class="brush: java;">
package org.zhizao.util;
/**
* 路径工具
* 2009-10-10
* @author IXR
* @version 1.0
*/
public class PathUtil {
/**
* 获取程序根目录磁盘路径(Java EE版)
* 注:如果是WEB项目在会返回WebRoot的根路径../
*    而不是../WEB-INF/classes/
* @return 根目录磁盘路径
*/
public static String getRootPath(){
String path = null;
try {
   path = PathUtil.class.getResource(&quot;/&quot;).toURI().getPath();
   path = path.substring(1, path.length()-1);
   String str = &quot;WEB-INF/classes&quot;;
   int i = path.indexOf(str);
   if(i &gt; 0){
    path = path.substring(0, i);
   }
} catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
return path;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://a18zhizao.com/y2010/1307_java-on-the-path-to-get-the-issue.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
