JSP项目中上传,怎样在Servlet中获得WebContent的绝对路径?

如题所述

第1个回答  2012-12-01
取得服务器相对路径
  System.getProperty("user.dir")
  例如:E:\apache-tomcat-5.5.16\apache-tomcat-5.5.16\bin

System.getProperty("user.dir").replace("bin","webapps")+"\你的工程名"就是WebContent的绝对路径追问

为什么我用System.getProperty("user.dir"),获得的是:
/home/james/桌面

追答

http://zhidao.baidu.com/question/340516870.html

这个里面有总结的,你看看。

追问

我知道我的问题所在了,现在是这样,我用Eclipse,项目的地址是临时的,怎样让Eclipse直接发布到自己想要的目录呢?

追答

可以选择换个workspace,File下面有个switch workspace。
换到你想要的地方就可以了。

追问

不是Workspace, 是指Tomcat启动后,项目发布的地址,比如,我在Servlet中获取文件绝对路径,得到的结果是:/home/XXXX/projects/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/XXXX/Servlet 这样的

本回答被网友采纳
第2个回答  2012-12-01
this.getServletContext.getRealPath(" 资源名") //返回资源在硬盘上的绝对路径本回答被提问者采纳
第3个回答  2012-12-01
String webBase = request.getServletContext().getRealPath("");
第4个回答  2012-12-03
页面上<%=request.getContextPath();%>java类中 request.getContextPath();
相似回答