在Servlet應(yīng)用中,有一個getRealPath(String str)的方法,這個方法盡管也可以動態(tài)地獲得文件的路徑,不秘直接手寫絕對路徑,但這也是一個不被建議使用的方法,那么,我們有什么方法可以更好地獲得文件呢?
????? 那就是Class.getResource()與Class.getResourceAsStream()方法,但很多人還是不太懂它的用法,因為很多人(比如不久前的我)都不知道應(yīng)該傳怎么樣的參數(shù)給它,當然,有些人己經(jīng)用得如火純青,這些人是不需要照顧的,在此僅給不會或者還不是很熟的人解釋一點點。
?
比如我們有以下目錄
|--project
??? |--src
??????? |--javaapplication
??????????? |--Test.java
?????????? ?|--file1.txt
??????? |--file2.txt
??? |--build?
??????? |--javaapplication
?????????? ?|--Test.class
??????????? |--file3.txt
??????? |--file4.txt
?
在上面的目錄中,有一個src目錄,這是JAVA源文件的目錄,有一個build目錄,這是JAVA編譯后文件(.class文件等)的存放目錄
那么,我們在Test類中應(yīng)該如何分別獲得
file1.txt? file2.txt? file3.txt? file4.txt這四個文件呢?
?
首先講file3.txt與file4.txt
file3.txt:
方法一:File file3 = new File(Test.class.getResource("file3.txt").getFile());
方法二:File file3 = new File(Test.class.getResource("/javaapplication/file3.txt").getFile());
方法三:File file3 = new File(Test.class.getClassLoader().getResource("javaapplication/file3.txt").getFile());
??
file4.txt:
方法一:File file4 = new File(Test.class.getResource("/file4.txt").getFile());
方法二:File file4 = new File(Test.class.getClassLoader().getResource("file4.txt").getFile());
??
很好,我們可以有多種方法選擇,但是file1與file2文件呢?如何獲得?
答案是,你只能寫上它們的絕對路徑,不能像file3與file4一樣用class.getResource()這種方法獲得,它們的獲取方法如下
假如整個project目錄放在c:/下,那么file1與file2的獲取方法分別為
file1.txt
方法一:File file1 = new File("c:/project/src/javaapplication/file1.txt");
方法二:。。。沒有
?
?
file2.txt
方法一:File file2 = new File("c:/project/src/file2.txt");
方法二:。。。也沒有
?
?
總結(jié)一下,就是你想獲得文件,你得從最終生成的.class文件為著手點,不要以.java文件的路徑為出發(fā)點,因為真正使用的就是.class,不會拿個.java文件就使用,因為java是編譯型語言嘛
?
至于getResouce()方法的參數(shù),你以class為出發(fā)點,再結(jié)合相對路徑的概念,就可以準確地定位資源文件了,至于它的根目錄嘛,你用不同的IDE build出來是不同的位置下的,不過都是以頂層package作為根目錄,比如在Web應(yīng)用中,有一個WEB-INF的目錄,WEB-INF目錄里面除了web.xml文件外,還有一個classes目錄,沒錯了,它就是你這個WEB應(yīng)用的package的頂層目錄,也是所有.class的根目錄 “/”,假如clasaes目錄下面有一個file.txt文件,它的相對路徑就是"/file.txt",如果相對路徑不是以"/"開頭,那么它就是相對于.class的路徑。。
?
還有一個getResourceAsStream()方法,參數(shù)是與getResouce()方法是一樣的,它相當于你用getResource()取得File文件后,再new InputStream(file)一樣的結(jié)果
來源: http://gavin-chen.iteye.com/blog/261151
?
public
URL
getResource(
String
?name)
ClassLoader.getSystemResource(java.lang.String)
。
在委托前,使用下面的算法從給定的資源名構(gòu)造一個絕對資源名:
- 如果 name 以 '/' ? 開始,則絕對資源名是 '/' 后面的 name 的一部分。
-
否則,絕對名具有以下形式:
modified_package_name / name其中 modified_package_name 是此對象的包名,該名用 '/' 取代了 '.' ( '\u002e' )。
Class.getResource(""); 獲取classpath
Class.getResource("JMF.class"); 代表獲取相于類路徑當前包下的SendService.class的類路徑.
/D:/bak/upload/upload/WebRoot/WEB-INF/classes/jmf/JMF.class-------->打印出的結(jié)果
Class.getResource("/jmf/WebCamSwing.class"); /jmf/WebCamSwing.class->代表相于類路徑的絕對路徑
file:/D:/bak/upload/upload/WebRoot/WEB-INF/classes/jmf/JMF.class -------->打印出的結(jié)果
?
?
我們怎么獲得Object的類路徑:
Class.getResource("/java/lang/Object.class")?因為Object是通過引導類加載器 (BootStrapClassLoader)加載的,所以此方法通過系統(tǒng)類加載器來查找資料,?所以我們要指定類的絕對路徑/java /lang/Object.class
public java.net.URL getResource(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
// A system class.
return ClassLoader.getSystemResource(name);
}
return cl.getResource(name);
}
?
?
我們來看看如何通過系統(tǒng)類加載器來查找Object:
Class.getClassLoader().getSystemResource("java/lang/Object.class")
?
?
打印出來的結(jié)果多是:
jar:file:/E:/Program/Java/jdk1.5.0_15/jre/lib/rt.jar!/java/lang/Object.class
?
為什么getResource("")前面要加"/",而getSystemResource("")前面不用加呢?
private String resolveName(String name) {
if (name == null) {
return name;
}
if (!name.startsWith("/")) {
Class c = this;
while (c.isArray()) {
c = c.getComponentType();
}
String baseName = c.getName();
int index = baseName.lastIndexOf('.');
if (index != -1) {
name = baseName.substring(0, index).replace('.', '/')
+"/"+name;
}
} else {
name = name.substring(1);
}
return name;
}
??
其實最后還是要把"/"去掉的...
來源 : http://wangxuliangboy.iteye.com/blog/345367
?
下面是我測試的結(jié)果:
?
上面為目錄結(jié)構(gòu),下面的方法是在類org.fileupload.servlet.SecondUploadServlet中測試的
System.out.println("this.getClass().getResource(*)--------------------------------");
System.out.println(this.getClass().getResource("")); // .../classes/org/fileupload/servlet/
System.out.println(this.getClass().getResource("/")); // .../classes/
// 編譯成class文件, 所以用.java是錯的, 應(yīng)用java
System.out.println(this.getClass().getResource("./FirstUploadServlet.java")); // null
System.out.println(this.getClass().getResource("/FirstUploadServlet.java")); // null
System.out.println(this.getClass().getResource("FirstUploadServlet.java")); // null
System.out.println(this.getClass().getResource("SecondUploadServlet.java")); // null
System.out.println(this.getClass().getResource("org.fileupload.servlet")); // null
System.out.println(this.getClass().getResource("org.fileupload.servlet.SecondUploadServlet.java")); // null
// ./或最前面沒有.和/表示當前目錄, /表示類路徑根目錄, 即classes目錄
System.out.println(this.getClass().getResource("./FirstUploadServlet.class")); // .../classes/org/fileupload/servlet/./FirstUploadServlet.class
System.out.println(this.getClass().getResource("/FirstUploadServlet.class")); // null
System.out.println(this.getClass().getResource("FirstUploadServlet.class")); // .../classes/org/fileupload/servlet/FirstUploadServlet.class
System.out.println(this.getClass().getResource("SecondUploadServlet.class")); // .../classes/org/fileupload/servlet/SecondUploadServlet.class
System.out.println(this.getClass().getResource("/org/fileupload/servlet")); // .../classes/org/fileupload/servlet/
System.out.println(this.getClass().getResource("org.fileupload.servlet.SecondUploadServlet.class")); // null
System.out.println(this.getClass().getResource("test")); // .../classes/org/fileupload/servlet/test/
System.out.println(this.getClass().getResource("./test")); // .../classes/org/fileupload/servlet/./test/
System.out.println(this.getClass().getResource("test/OK.java")); // null
System.out.println(this.getClass().getResource("/test/OK.java")); // null
System.out.println(this.getClass().getResource("./test/OK.java")); // null
System.out.println(this.getClass().getResource("test/OK.class")); // .../classes/org/fileupload/servlet/test/OK.class
System.out.println(this.getClass().getResource("/test/OK.class")); // null
System.out.println(this.getClass().getResource("./test/OK.class")); // .../classes/org/fileupload/servlet/./test/OK.class
System.out.println(this.getClass().getResource("/classpath.jsp")); // .../classes/classpath.jsp
System.out.println(this.getClass().getResource("./classpath.jsp")); // null
System.out.println(this.getClass().getResource("classpath.jsp")); // null
System.out.println("\n利用類加載器:");
System.out.println("this.getClass().getClassLoader().getResource(*)-------------");
System.out.println(this.getClass().getClassLoader().getResource("classpath.jsp"));
System.out.println(this.getClass().getClassLoader().getResource("FirstUploadServlet.class")); // null
System.out.println(this.getClass().getClassLoader().getResource("org/fileupload/servlet/FirstUploadServlet.class"));
System.out.println(this.getClass().getClassLoader().getResource("/org/fileupload/servlet/FirstUploadServlet.class"));
System.out.println(this.getClass().getClassLoader().getResource("./org/fileupload/servlet/FirstUploadServlet.class"));
System.out.println("\nrequest.getRealPath(*)--------------------------------------");
// 最前面有沒有加左斜桿結(jié)果基本是一樣的, 只是獲取根目錄時多或少了一右斜桿而已
// 如果最前面為./, 那么結(jié)果會變成\.\這樣的路徑
System.out.println(request.getRealPath("")); // ...\commons-fileupload
System.out.println(request.getRealPath("/")); // ...\commons-fileupload\
System.out.println(request.getRealPath("index.jsp")); // ...\commons-fileupload\index.jsp
System.out.println(request.getRealPath("./index.jsp")); // ...\commons-fileupload\.\index.jsp
System.out.println(request.getRealPath("/index.jsp")); // ...\commons-fileupload\index.jsp
System.out.println(request.getRealPath("fileupload/firstupload.jsp")); // ...\commons-fileupload\fileupload\firstupload.jsp
System.out.println(request.getRealPath("/fileupload/firstupload.jsp")); // ...\commons-fileupload\fileupload\firstupload.jsp
System.out.println(request.getRealPath("./fileupload/firstupload.jsp")); // ...\commons-fileupload\.\fileupload\firstupload.jsp
System.out.println(request.getRealPath("./fileupload")); // ...\commons-fileupload\.\fileupload
System.out.println(request.getRealPath("/fileupload")); // ...\commons-fileupload\fileupload
System.out.println(request.getRealPath("fileupload")); // ...\commons-fileupload\fileupload
// request.getSession().getServletContext().getRealPath與request.getRealPath結(jié)果一樣
System.out.println("\nrequest.getSession().getServletContext().getRealPath(*)------");
// System.out.println(request.getSession().getServletContext().getRealPath("/")); // ...\commons-fileupload\
System.out.println(request.getSession().getServletContext().getRealPath("")); // ...\commons-fileupload
System.out.println(request.getSession().getServletContext().getRealPath("/")); // ...\commons-fileupload\
System.out.println(request.getSession().getServletContext().getRealPath("index.jsp")); // ...\commons-fileupload\index.jsp
System.out.println(request.getSession().getServletContext().getRealPath("./index.jsp")); // ...\commons-fileupload\.\index.jsp
System.out.println(request.getSession().getServletContext().getRealPath("/index.jsp")); // ...\commons-fileupload\index.jsp
System.out.println(request.getSession().getServletContext().getRealPath("fileupload/firstupload.jsp")); // ...\commons-fileupload\fileupload\firstupload.jsp
System.out.println(request.getSession().getServletContext().getRealPath("/fileupload/firstupload.jsp")); // ...\commons-fileupload\fileupload\firstupload.jsp
System.out.println(request.getSession().getServletContext().getRealPath("./fileupload/firstupload.jsp")); // ...\commons-fileupload\.\fileupload\firstupload.jsp
System.out.println(request.getSession().getServletContext().getRealPath("./fileupload")); // ...\commons-fileupload\.\fileupload
System.out.println(request.getSession().getServletContext().getRealPath("/fileupload")); // ...\commons-fileupload\fileupload
System.out.println(request.getSession().getServletContext().getRealPath("fileupload")); // ...\commons-fileupload\fileupload
System.out.println("\nothers-------------------------------------------------------");
System.out.println(request.getServletPath()); // /servlet/SecondUploadServlet
System.out.println(request.getContextPath()); // /commons-fileupload
System.out.println(request.getRequestURI()); // /commons-fileupload/servlet/SecondUploadServlet
System.out.println(request.getRequestURL()); // http://localhost:8080/commons-fileupload/servlet/SecondUploadServlet
?
[ 轉(zhuǎn)自 : http://blog.sina.com.cn/s/blog_5b22f00a0100d2xu.html ]
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

