詳細(xì)介紹Java相對(duì)路徑與絕對(duì)路徑的問題
關(guān)于JAVA中相對(duì)路徑與絕對(duì)路徑的問題:
總結(jié)一下,遺漏的隨時(shí)補(bǔ)上
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目錄的全路徑
例如 : E:\eclipseM9/workspace/tree/WEB-INF/classes/ 這個(gè)方法也可以不在web環(huán)境里確定路徑,比較好用
3.request.getContextPath();
獲得web根的上下文環(huán)境 如 /tree tree是我的web項(xiàng)目的root context
1. 可以在servlet的init方法里 String path = getServletContext()。
getRealPath("/"); 這將獲取web項(xiàng)目的全路徑 例如 :E:\eclipseM9\workspace\tree\ tree是我web項(xiàng)目的根目錄
2 jsp 獲取文件路徑
- <%@ page contentType="text/html;
- charset=gb2312" language="java" import="java.io.*" errorPage="" %>
- <html> <head>
- <meta http-equiv="Content-Type"
- content="text/html;
- charset=gb2312">
- <title>Untitled Document</title>
- <head> <body>
當(dāng)前WEB應(yīng)用的物理路徑:
- <%=application.getRealPath("/")%>
- <BR> 當(dāng)前你求請(qǐng)的JSP文件的物理路徑:
- <%=application.getRealPath(request.getRequestURI())%><BR>
- <% String path=application.getRealPath(request.getRequestURI());
- String dir=new File(path).getParent();
- out.println("當(dāng)前JSP文件所在目錄的物理路徑"+dir);
- %>
- </body>
- </html>
- String virtPath = request.getServletPath();//虛擬路徑
- String realPath = request.getRealPath(virtPath);//物理路徑
JSP中獲得當(dāng)前應(yīng)用的相對(duì)路徑和絕對(duì)路徑
根目錄所對(duì)應(yīng)的絕對(duì)路徑:request.getRequestURI()
文件的絕對(duì)路徑 :application.getRealPath(request.getRequestURI());
當(dāng)前web應(yīng)用的絕對(duì)路徑 :application.getRealPath("/");
取得請(qǐng)求文件的上層目錄:new File(application.getRealPath(request.getRequestURI()))。getParent() Servlet中獲得當(dāng)前應(yīng)用的相對(duì)路徑和絕對(duì)路徑
根目錄所對(duì)應(yīng)的絕對(duì)路徑:request.getServletPath();
文件的絕對(duì)路徑 :request.getSession()。getServletContext()。getRealPath (request.getRequestURI()) 當(dāng)前web應(yīng)用的絕對(duì)路徑 :servletConfig.getServletContext()。getRealPath("/");
(ServletContext對(duì)象獲得幾種方式: Javax.servlet.http.HttpSession.getServletContext() Javax.servlet.jsp.PageContext.getServletContext() Javax.servlet.ServletConfig.getServletContext() )
Java 的Class中獲得相對(duì)路徑,絕對(duì)路徑的方法 單獨(dú)的Java類中獲得絕對(duì)路徑 根據(jù)Java.io.File的Doc文擋,可知: 默認(rèn)情況下new File("/")代表的目錄為:System.getProperty("user.dir")。
一下程序獲得執(zhí)行類的當(dāng)前路徑
- package org.cheng.file;
- import Java.io.File;
- public class FileTest {
- public static void main(String[] args) throws Exception {
- stem.out.println(Thread.currentThread().getContextClassLoader().getResource(""));
- System.out.println(FileTest.class.getClassLoader().getResource(""));
- System.out.println(ClassLoader.getSystemResource(""));
- System.out.println(FileTest.class.getResource(""));
- System.out.println(FileTest.class.getResource("/"));
- //Class文件所在路徑 System.out.println(new File("/").getAbsolutePath());
- System.out.println(System.getProperty("user.dir"));
- } }
服務(wù)器中的Java類獲得當(dāng)前路徑(來自網(wǎng)絡(luò))
(1)。Weblogic WebApplication的系統(tǒng)文件根目錄是你的weblogic安裝所在根目錄。
例如:如果你的weblogic安裝在c:\bea\weblogic700…… 那么,你的文件根路徑就是c:\. 所以,有兩種方式能夠讓你訪問你的服務(wù)器端的文件:
a.使用絕對(duì)路徑: 比如將你的參數(shù)文件放在c:\yourconfig\yourconf.properties, 直接使用 new FileInputStream("yourconfig/yourconf.properties");
b.使用相對(duì)路徑: 相對(duì)路徑的根目錄就是你的webapplication的根路徑,即WEB-INF的上一級(jí)目錄,將你的參數(shù)文件放 在yourwebapp\yourconfig\yourconf.properties, 這樣使用: new FileInputStream("./yourconfig/yourconf.properties"); 這兩種方式均可,自己選擇。
(2)。Tomcat 在類中輸出System.getProperty("user.dir");顯示的是%Tomcat_Home%/bin
(3)。Resin 不是你的JSP放的相對(duì)路徑,是JSP引擎執(zhí)行這個(gè)JSP編譯成SERVLET 的路徑為根。比如用新建文件法測(cè)試File f = new File("a.htm"); 這個(gè)a.htm在resin的安裝目錄下
(4)。如何讀相對(duì)路徑哪? 在Java文件中g(shù)etResource或getResourceAsStream均可
例:getClass()。getResourceAsStream(filePath);//filePath可以是"/filename",這里的/代表web 發(fā)布根路徑下WEB-INF/classes 默認(rèn)使用該方法的路徑是:WEB-INF/classes.已經(jīng)在Tomcat中測(cè)試。
希望通過本文的介紹,能給你帶來幫助。