获得WebRoot的路径:
request.getSession().getServletContext().getRealPath("/");
获得class的路径:
this.getClass().getClassLoader().getResource("").getPath();
获取服务器上WEBAPPS路径
public static String getRealWebappsPath(HttpServletRequest request) { String webPath = request.getSession().getServletContext().getRealPath( "/"); String basePath = webPath.substring(0, webPath.indexOf(request .getContextPath().substring(1))); return basePath; }