java獲取項目名
A. java如何獲取類的絕對路徑
1 用servlet獲取
1.1 獲取項目的絕對路徑
request.getSession().getServletContext().getRealPath("")
1.2 獲取瀏覽器地址
request.getRequestURL()
1.3 獲取當前文件的絕對路徑
request.getSession().getServletContext().getRealPath(request.getRequestURI())
2.獲取當前的classpath路徑
String a2=類名.class.getResource("").toString();
String a3=DBConnection.class.getResource("/").toString();
String a4=DBConnection.class.getClassLoader().getResource("").toString();
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
//輸出很好理解
3、獲取文件的絕對路徑
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=t.indexOf(".metadata");
String path=t.substring(1,num).replace('/', '\\')+"項目名\\WebContent\\文件";
B. JAVA中project name可以隨便取嘛
JAVA中project name不能隨便取。
有命名規范,項目名一般全小寫。名稱裂棚只能由字母、數字、下劃線、$符號組成,且不能以數塵鉛字開頭,也不能使用JAVA中的關鍵字。
另外,項目名一般取有意義的名肆兄則字,和項目有關聯。除此之外,可以自由取名。
C. Java web項目,在.java程序中如何獲取webapp路徑
String t=Thread.currentThread().getContextClassLoader().getResource("鏈嘩").getPath();
int num=t.indexOf(".metadata");
String path=t.substring(1,num).replace('/', '\\')+"項目名\\WebContent\散喚檔\文件";
復制,親測有效。沖亂
D. 請問在java代碼中如何獲得web項目名
項目名:
String path = request.getContextPath();
WEB路含純祥徑:
String basePath = request.getScheme()+"://"+request.getServerName()+":"褲如+request.getServerPort()+path+"/談搏";
E. java獲取請求域名
很多朋友都想知道java如何獲取請求域名?下面就一起來了解一下吧~
1、獲取協議名和域名。
request.getScheme(); //得到協議首此世名 例如:http request.getServerName(); //得到域名 localhost
2、獲取全路徑。
request.getRequestURL(); //得到http://localhost:8888/CRM/loginController/login
3、獲取請求所有參數 //map類型。
request.getParameterMap()
4、獲取項目名
request.getContextPath(); 者肢// /CRM
5、獲取請求方法
request.getServletPath(); // /loginController/login
/** * 獲取當前訪問URL (含協議、域名、埠號[忽略80埠]、項目名) * @param request * @return: String */ public static String getServerUrl(HttpServletRequest request) { // 訪問協議 String agreement = request.getScheme(); // 訪問域名 String serverName = request.getServerName(); // 訪問埠號 int port = request.getServerPort(); // 訪問項目名 String contextPath = request.getContextPath(); String url = "%s://%s%s%s"; 扒咐 String portStr = ""; if (port != 80) { portStr += ":" + port; } return String.format(url, agreement, serverName, portStr, contextPath); }
F. 在java類中怎麼獲得java項目的目錄
一 相對路徑的獲得
說明:相對路徑(即不寫明時候到底相對誰)均可通過以下方式獲得(不論是一般的java項目還是web項目)
String relativelyPath=System.getProperty("user.dir");
上述相對路徑中,java項目中的文件是相對於項目的根目錄
web項目中的文件路徑視不同的web伺服器不同而不同(tomcat是相對於 tomcat安裝目錄\bin)
二 類載入目錄的獲得(即當運行時某一類時獲得其裝載目錄)
1.1)通用的方法一(不論是一般的java項目還是web項目,先定位到能看到包路徑的第一級目錄)
InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
(test.txt文件的路徑為 項目名\src\test.txt;類TestAction所在包的第一級目錄位於src目錄下)
上式中將TestAction,test.txt替換成對應成相應的類名和文件名字即可
1.2)通用方法二 (此方法和1.1中的方法類似,不同的是此方法必須以'/'開頭,參考http://riddickbryant.iteye.com/blog/436693)
InputStream is=Test1.class.getResourceAsStream("/test.txt");
(test.txt文件的路徑為 項目名\src\test.txt,類Test1所在包的第一級目錄位於src目錄下)
三 web項目根目錄的獲得(發布之後)
1 從servlet出發
可建立一個servlet在其的init方法中寫入如下語句
ServletContext s1=this.getServletContext();
String temp=s1.getRealPath("/"); (關鍵)
結果形如:D:\工具\Tomcat-6.0\webapps\002_ext\ (002_ext為項目名字)
如果是調用了s1.getRealPath("")則輸出D:\工具\Tomcat-6.0\webapps\002_ext(少了一個"\")
2 從httpServletRequest出發
String cp11111=request.getSession().getServletContext().getRealPath("/");
結果形如:D:\工具\Tomcat-6.0\webapps\002_ext\
四 classpath的獲取(在Eclipse中為獲得src或者classes目錄的路徑)
方法一 Thread.currentThread().getContextClassLoader().getResource("").getPath()
eg: String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println("t---"+t);
輸出:t---/E:/order/002_ext/WebRoot/WEB-INF/classes/
方法二 JdomParse.class.getClassLoader().getResource("").getPath() (JdomParse為src某一個包中的類,下同)
eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);
輸出: JdomParse.class.getClassLoader().getResource--/E:/order/002_ext/WebRoot/WEB-INF/classes/
另外,如果想把文件放在某一包中,則可以 通過以下方式獲得到文件(先定位到該包的最後一級目錄)
eg String p2=JdomParse.class.getResource("").getPath();
System.out.println("JdomParse.class.getResource---"+p2);
輸出: JdomParse.class.getResource---/E:/order/002_ext/WebRoot/WEB-INF/classes/jdom/ (JdomParse為src目錄下jdom包中的類)
四 屬性文件的讀取:
方法 一
InputStream in = lnew BufferedInputStream( new FileInputStream(name)); Properties p = new Properties(); p.load(in);
注意路徑的問題,做執行之後就可以調用p.getProperty("name")得到對應屬性的值
方法二
Locale locale = Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle("test/propertiesTest", locale);
String value = localResource.getString("test");
System.out.println("ResourceBundle: " + value);
工程src目錄下propertiesTest.properties(名字後綴必須為properties)文件內容如下:
test=hello word
G. 普元EOS用java怎麼獲取項目的路徑的嗎
EOS項目中獲取當前構消畝件包配置目錄下文件路徑import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.eos.foundation.eoscommon.CommonUtil;
public abstract class {
public String (String configurationFileName) {
String packageName = CommonUtil.getCurrentContributionName();
final String FORMAT_STR = ".xml";
if(configurationFileName == null || "".equals(configurationFileName)) {
throw new IllegalArgumentException("構件包:"卜橋鎮 + packageName + " 的配置文件名不能為空!");
} else if(configurationFileName.length() < 5 ||
!FORMAT_STR.equals(configurationFileName.substring(configurationFileName.length() - 4, configurationFileName.length()))) {
throw new IllegalArgumentException("配置文件必須是XML格式的文件!");
}
String separator = File.separator;
String classesPath = .class.getClassLoader().getResource("").toString();
String classerDealPath = classesPath.substring(0, classesPath.length()-1);
String webInfPath = classerDealPath.substring(6, classerDealPath.lastIndexOf("/型粗"));
String packagePath = webInfPath + separator + "_srv"+separator+"work"+separator+"user" +separator+ packageName;
String configFileAbsolutePath = packagePath + separator + "META-INF" + separator + configurationFileName;
return configFileAbsolutePath;
}
protected FileInputStream (String configurationAbsolutePath) throws FileNotFoundException {
File file = new File(configurationAbsolutePath);
return new FileInputStream(file);
}
}
H. java windows和linux獲取項目根目錄的方法一致嗎
java有個特性是跨平台性,所以其獲取項目根目錄的方法是一樣的。
request.getContextPath()方法就是是得到項目的名字,如果項目為根目錄,則得到一個"",即空的字條串。如果項目為abc,<%=request.getContextPath()%> 將得到abc,伺服器端的路徑則會自動加上,<a href="XXXX.jsp"> 是指當前路徑下的這個xxx.jsp頁面,有時候也可以在head里設置html:base來解決路徑的問題,不多用的最多的還是request.getContextPath。
I. java request 如何取到發送請求的地址是什麼
request對象通過以下方法來獲取請求路徑,如下所示:
String getServerName():獲取伺服器名,localhost;
String getServerPort():獲取伺服器埠號,8080;
String getContextPath():獲取項目名,/Example;
String getServletPath():碰舉獲取Servlet路譽態徑,/AServlet;
String getQueryString():獲取參數部分,即問號後面的部笑虛碧分:username=zhangsan
String getRequestURI():獲取請求URI,等於項目名+Servlet路徑:/Example/AServlet
String getRequestURL():獲取請求URL,等於不包含參數的整個請求路徑:http://localhost:8080/Example/AServlet 。
J. 通過java獲取當前項目路徑
getClass().getResource() 方法獲得相對路徑( 此方法在jar包中無效。返回的內容最後包含/)
例如 項目在/D:/workspace/MainStream/Test
在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/
publicStringgetCurrentPath(){
//取得根目錄路徑
StringrootPath=getClass().getResource("/").getFile().toString();
//當前目錄路徑
StringcurrentPath1=getClass().getResource(".").getFile().toString();
StringcurrentPath2=getClass().getResource("").getFile().toString();
//當前目錄的上級目錄路徑
StringparentPath=getClass().getResource("../").getFile().toString();
returnrootPath;
}