當前位置:首頁 » 網頁前端 » web工程獲取路徑
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

web工程獲取路徑

發布時間: 2022-04-13 09:56:16

A. 如何在java web項目中獲得相對路徑

第一步: 先獲得classpath路徑

Stringclasspath=this.getClass().getResource("/").getPath().replaceFirst("/","");

這樣子可以得到classpath路徑,類似於:

F:/projects/JavaStudyParent/study-springmvc-junit-test/target/springmvc-junit-test/WEB-INF/classes/


然後把WEB-INF/classes截取就能獲得WebAPP目錄啦:

StringwebappRoot=classpath.replaceAll("WEB-INF/classes/","");

得到的結果就是:

F:/projects/JavaStudyParent/study-springmvc-junit-test/target/springmvc-junit-test/


通過這個路徑你就能獲取該文件夾下的所有文件啦

B. Java web項目,在.java程序中如何獲取webapp路徑

String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=t.indexOf(".metadata");
String path=t.substring(1,num).replace('/', '\\')+"項目名\\WebContent\\文件";
復制,親測有效。

C. javaweb中怎麼獲取文件的絕對路徑

在jsp和class文件中調用的相對路徑不同。 在jsp里,根目錄是WebRoot 在class文件中,根目錄是WebRoot/WEB-INF/classes 當然你也可以用System.getProperty("user.dir")獲取你工程的絕對路徑。
另:在Jsp,Servlet,Java中詳細獲得路徑的方法!
1.jsp中取得路徑:

以工程名為TEST為例:

(1)得到包含工程名的當前頁面全路徑:request.getRequestURI()
結果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
結果:/TEST
(3)得到當前頁面所在目錄下全名稱:request.getServletPath()
結果:如果頁面在jsp目錄下 /TEST/jsp/test.jsp
(4)得到頁面所在伺服器的全路徑:application.getRealPath("頁面.jsp")
結果:D:/resin/webapps/TEST/test.jsp
(5)得到頁面所在伺服器的絕對路徑:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
結果:D:/resin/webapps/TEST

2.在類中取得路徑:

(1)類的絕對路徑:String u=Class.class.getClass().getResource("/").getPath()
結果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路徑:System.getProperty("user.dir")
結果:D:/TEST

3.在Servlet中取得路徑:

(1)得到工程目錄:request.getSession().getServletContext().getRealPath("") 參數可具體到包名。
結果:E:/Tomcat/webapps/TEST
(2)得到IE地址欄地址:request.getRequestURL()

(3)得到相對地址:request.getRequestURI()
結果:/TEST/test

D. 如何獲取java web項目的工程路徑

request.getSession().getServletContext().getRealPath("/");//獲取web項目的路徑

this.getClass().getResource("/").getPath()//獲取類的當前目錄

E. java怎麼取到web服務的根路徑

java獲取根路徑有兩種方式:

1)在servlet可以用一下方法取得:
request.getRealPath(「/」)
例如:filepach = request.getRealPath(「/」)+」//upload//」;
2)不從jsp,或servlet中獲取,只從普通java類中獲取:
String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
SAXReader() saxReader = new SAXReader();
if(path.indexOf(「WEB-INF」)>0){
path = path.substring(0,path.indexOf(「/WEB-INF/classes」)+16);
// 『/WEB-INF/classes』為16位
document = saxReader.read(path+filename);
}else{
document = saxReader.read(getClass().getResourceAsStream(filename));
}

F. javaweb工程中,我想獲取WEB-INF中的a.properties中的內容,但他的路徑怎麼獲取啊

在action或者servlet中可以通過request.getContextPath()可以獲取項目跟路徑:
例如項目部署在F:\Program Files\apache-tomcat-6.0.26\webapps\項目名稱
那麼獲取的就是:F:\Program Files\apache-tomcat-6.0.26\webapps\項目名稱
這樣只要在後面加入你指定的目錄:
request.getContextPath()+「/WEB-INF」+"/a.properties"+

G. 如何獲取web應用的部署路徑

在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,獲取不到路徑,目前還沒有找到具體的解決方案。

H. J2EE web項目,絕對路徑和相對路徑如何設置與獲取

在開發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的物理路徑。

I. java web工程,讀取配置文件路徑問題

讀取配置文件 , xxx.properties放在webroot/WEB-INF/classes/目錄下

首先將配置文件轉換成InputStream,有兩種方式,原理一樣,都是通過類載入器得到資源:

(1)InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("xx.properties");
(2) InputStream inputStream =
this.getClass() .getClassLoader().getResourceAsStream( "xx.properties" );
調用對象的getClass()方法是獲得對象當前的類類型,這部分數據存在方法區中,
而後在類類型上調用 getClassLoader()方法是得到當前類型的類載入器,我們知道在Java中所有的類都是通過載入器載入到虛擬機中的,而且類載入器之間存在父 子關系,就是子知道父,父不知道子,這樣不同的子載入的類型之間是無法訪問的(雖然它們都被放在方法區中),所以在這里通過當前類的載入器來載入資源也就 是保證是和類類型同一個載入器載入的。
最後調用了類載入器的getResourceAsStream()方法來載入資源。

(3) 然後載入配置文件,讀取屬性值
Properties prop = new Properties();
prop.load(input);
String value = prop.getProperty("PropertyName");

input.close();