⑴ Web項目獲取資源路徑問題 相對路徑和絕對路徑
把 ../ 去了
直接 js/jquery-1.8.3.js
因為你的gmtop.jsp跟js是同級的 所以相對路徑就不用往上級跳了
⑵ JAVA Web獲取路徑幾種方式
1、獲取項目根路徑
req.getSession().getServletContext().getRealPath("/");
2、獲取類路徑
this.getClass().getResource("/").getPath();
⑶ spring boot怎麼獲取web路徑
1. 默認位置: Spring Boot能大大簡化WEB應用開發的原因, 最重要的就是遵循「約定優於配置」這一基本原則。Spring Boot的關於靜態資源的默認配置已經完全滿足絕大部分WEB應用的需求。沒必要去弄手續繁雜的自定義,用Spring Boot的約定就好了。
⑷ 如何獲取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,獲取不到路徑,目前還沒有找到具體的解決方案。
⑸ 普通JAVA類 如何獲取,WEB項目的根路徑
在提問前,你應該選搜索下。web項目的路徑問題已有過回答了。貼個代碼片段給你吧……,希望能對你有所幫助
/**
* 獲取項目classpath目錄的絕對路徑
*
* @return classes目錄的絕對路徑<br/>
* file:/F:/tomcat/webapps/J2EEUtil/WEB-INF/classes/
* */
public static URL getAbsolutePathWithClass() {
return WebPath.class.getResource("/");
}
/**
* 獲取項目classPath目錄下的指定目錄的絕對路徑
*
* @param path
* classes目錄下的指定目錄.比如:/com/
* @return file:/F:/tomcat/webapps/J2EEUtil/WEB-INF/classes/com/
* */
public static URL getAbsolutePathWithClass(String path) {
return WebPath.class.getResource(path);
}
/**
* 獲取指定類文件的所在目錄的絕對路徑
*
* @param clazz
* 類
* @return 類文件的絕對路徑.例如:<br/> 包com.Aries.Util.Web下的Main.java類.<br/>
* 路徑為:file:/
* F:/tomcat/webapps/J2EEUtil/WEB-INF/classes/com/Aries/Util/Web/
* */
public static URL getAbsolutePathWithClass(Class clazz) {
return clazz.getResource("");
}
}
⑹ 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));
}
⑺ 如何在web頁面獲取文件絕對路徑
Server.MapPath
⑻ 如何獲取java web項目的工程路徑
request.getSession().getServletContext().getRealPath("/");//獲取web項目的路徑
this.getClass().getResource("/").getPath()//獲取類的當前目錄
⑼ java獲取web路徑問題
改成這樣既可
public class DBPath
{
public String getFileFolder(){
return this.getClass().getClassLoader().getResource("").getPath();
}
public static void main(String args[]){
DBPath d=new DBPath ();
System.out.println(d.getFileFolder());
}
}
getResource("/")時返回null,所以報錯。像我這樣就不會。。