您的位置:首页 > 编程语言 > Java开发

JAVA 得到上下文绝对路径

2013-12-19 01:37 218 查看
/* 第一种:在 Struts2 当中,Struts1同样也可通过这种形式获取: */
/* 获取 request 对象 */
HttpServletRequest request = ServletActionContext.getRequest();
/* 得到当前上下文绝对路径以保存图片 */
String contextPath = request.getSession().getServletContext().getRealPath("//");

/* 第二种:在 Filter 当中: */
public void init(FilterConfig config) throws ServletException {
/* 可将此参数放入到 Session 可供全局使用 */
String contextPath = config.getServletContext().getRealPath("//");
}

/* 第三种 */
String contextPath = request.getScheme()+"://"+request.getServerName()    +":"+request.getServerPort()+request.getContextPath()+"/";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: