SSH2里怎么获取项目的绝对路径

zy582sc 2012-08-14 10:36:37
dir=System.getProperty("user.dir");的结果是F:\java\JDK\eclipse
this.getClass().getResource("/").getPath();的结果是/E:/eclipse%20Workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/test/WEB-INF/classes/
相对来说第二句准确一点,第一完全不着边了,但是我想要项目的路径不知道怎么弄
...全文
486 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
YECHWNG 2012-08-16
  • 打赏
  • 举报
回复
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
dddsfdfgfdgfdg 2012-08-16
  • 打赏
  • 举报
回复
request.getSession().getServletContext().getRealPath("/");
kuailexiaobuding4 2012-08-16
  • 打赏
  • 举报
回复
楼上的同学们解释够用了。
s478853630 2012-08-14
  • 打赏
  • 举报
回复
你那个是classpath
项目的路径是这样的:request.getSession().getServletContext().getRealPath("/");
monrove 2012-08-14
  • 打赏
  • 举报
回复
//获取应用的上文路径
org.apache.struts2.ServletActionContext.getServletContext().getContextPath();

/获取应用在硬盘上的根目录
org.apache.struts2.ServletActionContext.getServletContext().getRealPath("/");
Iamagril 2012-08-14
  • 打赏
  • 举报
回复
学习了,一直没那样写过
rj0917 2012-08-14
  • 打赏
  • 举报
回复
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

这个就是项目的绝对路径,一般在Jsp页面中使用它来作为基准路径basePath
凤鸣86 2012-08-14
  • 打赏
  • 举报
回复
public class Test {
public void test(){
System.out.println(this.getClass().getClassLoader().getResource("").getPath());
}
public static void main(String[] args) {
Test test = new Test();
test.test();
}
}

this.getClass().getClassLoader().getResource("").getPath()使用servlet,jsp
Sammie 2012-08-14
  • 打赏
  • 举报
回复
JSP页面获取路径:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

Class:

//getRealPath里面可选参数
System.out.println("项目的绝对路径:"+request.getSession().getServletContext().getRealPath(""));
System.out.println("相对路径:"+request.getRequestURI());

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧