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

Java得到上下文的绝对路径

2011-01-11 23:36 363 查看
1. /* 第一种:在 Struts2 当中,Struts1同样也可通过这种形式获取: */
2. /* 获取 request 对象 */
3. HttpServletRequest request = ServletActionContext.getRequest();
4. /* 得到当前上下文绝对路径以保存图片 */
5. String contextPath = request.getSession().getServletContext().getRealPath("//");
6.
7. /* 第二种:在 Filter 当中: */
8. public void init(FilterConfig config) throws ServletException {
9.   /* 可将此参数放入到 Session 可供全局使用 */
10.   String contextPath = config.getServletContext().getRealPath("//");
11.  }
12.
13. /* 第三种 */
14. String contextPath = request.getScheme()+"://"+request.getServerName()    +":"+request.getServerPort()+request.getContextPath()+"/";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: