JavaEE 絕對路徑、相對路徑擷取方式__Java

來源:互聯網
上載者:User
  Java中使用的路徑,分為兩種:絕對路徑和相對路徑。歸根結底,Java本質上只能使用絕對路徑來尋找資源。所有的相對路徑尋找資源的方法,都不過是一些便利方法。不過是API在底層協助我們構建了絕對路徑,從而找到資源的。  在開發Web方面的應用時, 經常需要擷取伺服器中當前WebRoot的實體路徑。  如果是Servlet , Action , Controller, 或者Filter , Listener , 攔截器等相關類時, 我們只需要獲得ServletContext, 然後通過ServletContext.getRealPath("/")來擷取當前應用在伺服器上的物理地址。  如果在類中取不到ServletContext時,有兩種方式可以做到:  1)利用Java的類載入機制:調用 XXX.class.getClassLoader().getResource(""); 方法來擷取到ClassPath , 然後處理獲得WebRoot目錄。  這種方式只能是該class在WebRoot/WEB-INF/classes下才會生效, 如果該class被打包到一個jar檔案中, 則該方法失效。這時就應該用下面一種方式。  2)spring架構的思路,在WEB-INF/web.xml中,建立一個webAppRootKey的param,指定一個值(預設為webapp.root)作為索引值,然後通過Listener, 或者Filter,或者Servlet 執行String webAppRootKey = getServletContext().getRealPath("/"); 並將webAppRootKey對應的webapp.root 分別作為Key,Value寫到System Properties系統屬性中。之後在程式中通過System.getProperty("webapp.root")來獲得WebRoot的實體路徑。  根據第二種的思路,我們還可以再擴充一下。不過對於在部署在一台伺服器中的應用來說,若還不是你所需請再往下看。  下面是一些得到classpath和當前類的絕對路徑的一些方法。你可使用其中的一些方法來得到你需要的資源的絕對路徑:  1.DebitNoteAction.class.getResource("")  得到的是當前類FileTest.class檔案的URI目錄。不包括自己。  如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/  atacarnet/src/com/evi/modules/atacarnet/action/  2.DebitNoteAction.class.getResource("/")  得到的是當前的classpath的絕對URI路徑。  如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/  3.Thread.currentThread().getContextClassLoader().getResource("")  得到的也是當前ClassPath的絕對URI路徑  如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/  推薦使用該方法擷取。  4.DebitNoteAction.class.getClassLoader().getResource("") 或ClassLoader.getSystemResource("")  得到的也是當前ClassPath的絕對URI路徑。  如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/  5.取得伺服器相對路徑  System.getProperty("user.dir")  例如:E:\apache-tomcat-5.5.16\apache-tomcat-5.5.16\bin  6.取得項目中的絕對路徑  一般用request.getRealPath("/")或request.getRealPath("/config/")  但現在不提倡使用request.getRealPath("/")了,大家可試用ServletContext.getRealPath("/")方法得到Web應用程式的根目錄的絕對路徑。  要取得src的檔案非常容易,因為src是預設的相對目錄,比如你說要取得src下com目錄的test.java檔案,你只需要這樣就夠了  File f = new File(com/test.java);  但如果我要取得不在src目錄或者WebRoot目錄下的檔案呢,而是要從src或者WebRoot同級的目錄中取呢,比如說doc吧。  我的硬方法是這樣實現的:  String path = this.getServletContext().getRealPath("/");  Properties p = new Properties();  p.load(new FileInputStream(new File(path.substring(0,(path.lastIndexOf("\\WebRoot") + 1)) + "doc/db.properties")));  System.out.println(p.getProperty("driverName"));-------------------------------另:Request中getContextPath、getServletPath、getRequestURI、getRequestURL、getRealPath的區別  假定你的web application 名稱為news,你在瀏覽器中輸入請求路徑:http://localhost:8080/news/main/list.jsp  則執行下面向行代碼後列印出如下結果:1、 System.out.println(request.getContextPath());  列印結果:/news2、System.out.println(request.getServletPath());  列印結果:/main/list.jsp3、 System.out.println(request.getRequestURI());  列印結果:/news/main/list.jsp4、System.out.println(request.getRequestURL());  列印結果:http://localhost:8080/news/main/list.jsp5、 System.out.println(request.getRealPath("/"));  列印結果:F:\Tomcat 6.0\webapps\news\test

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.