1.基本概念的理解絕對(duì)路徑:絕對(duì)路徑就是你的主頁上的文件或目錄在硬盤上真正的路徑,(URL和物理路徑)例如:C:xyz est.txt 代表了test.txt文件的絕對(duì)路徑。http://www.sun.com/index.htm也代表了一個(gè)URL絕對(duì)路徑。相對(duì)路徑:相對(duì)與某個(gè)基準(zhǔn)目錄的路徑。包含Web的相對(duì)路徑(HTML中的相對(duì)目錄),例如:在Servlet中,"/"代表Web應(yīng)用的跟目錄。和物理路徑的相對(duì)表示。例如:"./" 代表當(dāng)前目錄,"../"代表上級(jí)目錄。這種類似的表示,也是屬于相對(duì)路徑。另外關(guān)于URI,URL,URN等內(nèi)容,請(qǐng)參考RFC相關(guān)文檔標(biāo)準(zhǔn)。RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax,(http://www.ietf.org/rfc/rfc2396.txt)2.關(guān)于JSP/Servlet中的相對(duì)路徑和絕對(duì)路徑。2.1服務(wù)器端的地址服務(wù)器端的相對(duì)地址指的是相對(duì)于你的web應(yīng)用的地址,這個(gè)地址是在服務(wù)器端解析的(不同于html和javascript中的相對(duì)地址,他們是由客戶端瀏覽器解析的)
1、request.getRealPath
方法:request.getRealPath("/")
得到的路徑:C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\strutsTest\
方法:request.getRealPath(".")
得到的路徑:C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\strutsTest\.
方法:request.getRealPath("")
得到的路徑:C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\strutsTest
request.getRealPath("web.xml")
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\strutsTest\web.xml
2、request.getParameter("");
ActionForm.getMyFile();
方法:String filepath = request.getParameter("myFile");
得到的路徑:D:\VSS安裝目錄\users.txt
方法:String filepath = ActionForm.getMyFile();
得到的路徑:D:\VSS安裝目錄\users.txt
--------------------------------------------------
strutsTest 為工程名
myFile 在ActionForm中,為private String myFile;
在jsp頁面中:為<html:file property="myFile"></html:file>
--------------------------------------------------
3、獲得系統(tǒng)路徑
在Application中:
System.getProperty("user.dir")
在Servlet中:
ServletContext servletContext = config.getServletContext();
String rootPath = servletContext.getRealPath("/");
在jsp中:
application.getRealPath("")
4、其他
1.可以在servlet的init方法里String path = getServletContext().getRealPath("/");這將獲取web項(xiàng)目的全路徑例如 :E:\eclipseM9\workspace\tree\tree是我web項(xiàng)目的根目錄2.你也可以隨時(shí)在任意的class里調(diào)用this.getClass().getClassLoader().getResource("").getPath();這將獲取 到classes目錄的全路徑例如 : /D:/workspace/strutsTest/WebRoot/WEB-INF/classes/
還有 this.getClass().getResource("").getPath().toString();
這將獲取 到 /D:/workspace/strutsTest/WebRoot/WEB-INF/classes/bl/
這個(gè)方法也可以不在web環(huán)境里確定路徑,比較好用3.request.getContextPath();獲得web根的上下文環(huán)境如 /treetree是我的web項(xiàng)目的root context
5、其他12
java獲取路徑幾種途徑- -
jdk如何判斷程序中的路徑呢?一般在編程中,文件路徑分為相對(duì)路徑和絕對(duì)路徑,絕對(duì)路徑是比較好處理的,但是不靈活,因此我們?cè)诰幊讨袑?duì)文件進(jìn)行操作的時(shí)候,一般都是讀取文件的相對(duì)路徑,相對(duì)路徑可能會(huì)復(fù)雜一點(diǎn),但是也是比較簡(jiǎn)單的,相對(duì)的路徑,主要是相對(duì)于誰,可以是類加載器的路徑,或者是當(dāng)前java文件下的路徑,在jsp編程中可能是相對(duì)于站點(diǎn)的路徑,相對(duì)于站點(diǎn)的路徑,我們可以通過getServletContext().getRealPath("\") 和request.getRealPath("\"):這個(gè)是取得站點(diǎn)的絕對(duì)路徑; 而getContextPath():取得站點(diǎn)的虛擬路徑;2:class.getClassLoader.getPath():取得類加載器的路徑:什么是類加載器呢?一般類加載器有系統(tǒng)的和用戶自己定義的;系統(tǒng)的ClassLoader就是jdk提供的,他的路徑就是jdk下的路徑,或者在 jsp編程,比如Tomcat ,取得的類加載器的位置就是tomaca自己設(shè)計(jì)的加載器的路徑,明白了這些之后,對(duì)于文件路徑的操作就會(huì)相當(dāng)?shù)那宄?,我們?cè)诰幊痰臅r(shí)候,只要想清楚我們所操作的文件是相對(duì)于什么路徑下的,取得相對(duì)路徑就可以了.
6、總結(jié)
1、獲取web服務(wù)器下的文件路徑
request.getRealPath("/")
application.getRealPath("")【jsp中 】
ServletContext().getRealPath("")
System.getProperty("user.dir")【不同位置調(diào)用,獲取的路徑是動(dòng)態(tài)變化的】
2、獲取本地路徑
jsp中,<html:file property="myFile"/>
request.getParameter("myFile");
ActionForm.getMyFile();
獲取的值相同:如D:\VSS安裝目錄\users.txt
*********************************
this.getClass().getClassLoader().getResource("").getPath();
==/D:/workspace/strutsTest/WebRoot/WEB-INF/classes/
this.getClass().getResource("").getPath().toString();
?。剑?D:/workspace/strutsTest/WebRoot/WEB-INF/classes/bl/
3、獲取相對(duì)路徑
request.getContextPath();
如:/strutsTest
帖二:http://teamojiao.javaeye.com/blog/446615
關(guān)于絕對(duì)路徑和相對(duì)路徑:
絕對(duì)路徑就是你的主頁上的文件或目錄在硬盤上真正的路徑,(URL和物理路徑)例如:C:xyz est.txt 代表了test.txt文件的絕對(duì)路徑。http://www.sun.com/index.htm也代表了一個(gè)URL絕對(duì)路徑。相對(duì)路徑:相對(duì)與某個(gè)基準(zhǔn)目錄的路徑。包含Web的相對(duì)路徑(HTML中的相對(duì)目錄),例如:在Servlet中,"/"代表Web應(yīng)用的跟目錄。和物理路徑的相對(duì)表示。例如:"./" 代表當(dāng)前目錄,"../"代表上級(jí)目錄。這種類似的表示,也是屬于相對(duì)路徑。另外關(guān)于URI,URL,URN等內(nèi)容,請(qǐng)參考RFC相關(guān)文檔標(biāo)準(zhǔn)。RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax,(http://www.ietf.org/rfc/rfc2396.txt)2.關(guān)于JSP/Servlet中的相對(duì)路徑和絕對(duì)路徑。 2.1服務(wù)器端的地址服務(wù)器端的相對(duì)地址指的是相對(duì)于你的web應(yīng)用的地址,這個(gè)地址是在服務(wù)器端解析的(不同于html和javascript中的相對(duì)地址,他們是由客戶端瀏覽器解析的)
第一種:
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
結(jié)果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin
獲取當(dāng)前類的所在工程路徑;
如果不加“/”
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);
結(jié)果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
獲取當(dāng)前類的絕對(duì)路徑;
第二種:
File directory = new File("");//參數(shù)為空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);
結(jié)果:
C:\Documents and Settings\Administrator\workspace\projectName
獲取當(dāng)前類的所在工程路徑;
第三種:
URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);
結(jié)果:
file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
獲取當(dāng)前工程src目錄下selected.txt文件的路徑
第四種:
System.out.println(System.getProperty("user.dir"));
結(jié)果:
C:\Documents and Settings\Administrator\workspace\projectName
獲取當(dāng)前工程路徑
第五種:
System.out.println( System.getProperty("java.class.path"));
結(jié)果:
C:\Documents and Settings\Administrator\workspace\projectName\bin
獲取當(dāng)前工程路徑
聯(lián)系客服