當前位置:首頁 » 文件管理 » java獲取上傳路徑

java獲取上傳路徑

發布時間: 2025-03-24 15:17:07

java讀取文件路徑問題

在java中獲得文件的路徑在我們做上傳文件操作時是不可避免的。

web 上運行
1:
this.getClass().getClassLoader().getResource("/").getPath();
this.getClass().getClassLoader().getResource("").getPath(); 得到的是 ClassPath的絕對URI路徑。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
System.getProperty("user.dir");
this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 項目的絕對路徑。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

2:
this.getClass().getResource("/").getPath();
this.getClass().getResource("").getPath(); 得到的是當前類 文件的URI目錄。不包括自己!
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/
this.getClass().getResource(".").getPath(); X 不 能運行

3:
Thread.currentThread().getContextClassLoader().getResource("/").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的絕對URI路徑。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
Thread.currentThread().getContextClassLoader().getResource(".").getPath() 得到的是 項目的絕對路徑。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

在本地運行中
1:
this.getClass().getClassLoader().getResource("").getPath();
this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 ClassPath的絕對URI路徑。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
this.getClass().getClassLoader().getResource(".").getPath(); X 不 能運行
2:
this.getClass().getResource("").getPath();
this.getClass().getResource(".").getPath(); 得到的是當前類 文件的URI目錄。不包括自己!
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/
/D:/myProjects/hp/WebRoot/WEB-INF/classes/ 得到的是 ClassPath的絕對URI路徑。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes

3:
Thread.currentThread().getContextClassLoader().getResource(".").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的絕對URI路徑。。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
Thread.currentThread().getContextClassLoader().getResource("/").getPath() X 不 能運行

最後
在Web應用程序中,我們一般通過ServletContext.getRealPath("/")方法得到Web應用程序的根目錄的絕對路徑。
還有request.getContextPath(); 在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic伺服器,項目內部並沒有文件結構的概念,用這種方式是始終得到null,獲取不到路徑,目前還沒有找到具體的解決方案。

熱點內容
Char在sql 發布:2025-07-10 10:19:19 瀏覽:780
請密碼不叫什麼說話 發布:2025-07-10 10:06:22 瀏覽:113
蘋果應用怎麼設置密碼 發布:2025-07-10 10:04:00 瀏覽:838
雪國腳本 發布:2025-07-10 10:04:00 瀏覽:937
編程讓 發布:2025-07-10 09:48:13 瀏覽:359
資料庫邏輯存儲結構 發布:2025-07-10 09:26:56 瀏覽:920
密碼編譯找規律 發布:2025-07-10 09:18:10 瀏覽:512
電影視頻緩存後 發布:2025-07-10 09:16:48 瀏覽:894
伺服器搭建需要哪些東西 發布:2025-07-10 09:15:23 瀏覽:803
無限密碼怎麼改 發布:2025-07-10 09:14:32 瀏覽:106