springMVC获取项目真实路径

1、原生httpservletrequest

例如访问地址是:http://localhost:8080/test

request.getContextPath(); 获取到工程名称:    /test

request.getServletContext().getRealPath("/");  获取到实际路径例如:E:\Java\tomcat7\apache-tomcat-7.0.55\wtpwebapps\test\



2、shiro

shiro可以直接从shiro中获取request和session

    	ServletRequest request = ((WebSubject)SecurityUtils.getSubject()).getServletRequest();   
    	HttpSession httpSession = ((HttpServletRequest)request).getSession();   
    	logger.debug("httpSession.getServletContext():"+httpSession.getServletContext());  
    	System.out.println(httpSession.getServletContext().getRealPath("/WEB-INF"));


你可能感兴趣的:(springmvc,spring)