本文目錄一覽:
- 1、java 項目如何獲取項目所在的物理根路徑
- 2、java中類加載路徑和項目根路徑獲取有幾種方式?
- 3、java項目根目錄和類路徑問題
- 4、java怎麼取到web服務的根路徑
- 5、java項目路徑文件怎麼寫
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.war2: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/classesthis.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/classes3:Thread.currentThread().getContextClassLoader().getResource(“.”).getPath()Thread.currentThread().getContextClassLoader().getResource(“”).getPath()得到的是ClassPath的絕對URI路徑。。如:/D:/myProjects/hp/WebRoot/WEB-INF/classesThread.currentThread().getContextClassLoader().getResource(“/”).getPath()X不能運行最後在Web應用程序中,我們一般通過ServletContext.getRealPath(”/”)方法得到Web應用程序的根目錄的絕對路徑。還有request.getContextPath();在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic服務器,項目內部並沒有文件結構的概念,用這種方式是始終得到null,獲取不到路徑,目前還沒有找到具體的解決方案。
java中類加載路徑和項目根路徑獲取有幾種方式?
package my;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class MyUrlDemo {
public static void main(String[] args) {
MyUrlDemo muDemo = new MyUrlDemo();
try {
muDemo.showURL();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void showURL() throws IOException {
// 第一種:獲取類加載的根路徑 D:\git\daotie\daotie\target\classes
File f = new File(this.getClass().getResource(“/”).getPath());
System.out.println(f);
// 獲取當前類的所在工程路徑; 如果不加「/」 獲取當前類的加載目錄 D:\git\daotie\daotie\target\classes\my
File f2 = new File(this.getClass().getResource(“”).getPath());
System.out.println(f2);
// 第二種:獲取項目路徑 D:\git\daotie\daotie
File directory = new File(“”);// 參數為空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);
// 第三種: file:/D:/git/daotie/daotie/target/classes/
URL xmlpath = this.getClass().getClassLoader().getResource(“”);
System.out.println(xmlpath);
// 第四種: D:\git\daotie\daotie
System.out.println(System.getProperty(“user.dir”));
/*
* 結果: C:\Documents and Settings\Administrator\workspace\projectName
* 獲取當前工程路徑
*/
// 第五種: 獲取所有的類路徑 包括jar包的路徑
System.out.println(System.getProperty(“java.class.path”));
}
}
java項目根目錄和類路徑問題
java獲取src目錄下文件夾的相對路徑問題如下:
目錄結構:
project
out
src
read.java
test.txt
files
opts
項目為priject
out目錄為.class輸出目錄
src下為文件目錄
src下有兩個包,files、opts
想通過相對路徑獲取test.txt的路徑
但是用反射只能獲取到.class,也就是out里的路徑
輸出後的目錄不就是在out裏面了,那個裏面的和src裏面的文件是一樣的, getClass().getResource()就可以得到classpath了啊
看看設置的資源文件編譯路徑
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));
}
java項目路徑文件怎麼寫
有絕對路徑與相對路徑兩種:
絕對路徑 :以引用文件之網頁所在位置為參考基礎,而建立出的目錄路徑。
絕對路徑:以Web站點根目錄為參考基礎的目錄路徑。
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/130736.html