JAVA中获取绝对路径一般有这几个方法:

request.getRealPath()// 此方法已过期,不推荐使用
this.getServletContext().getRealPath() //SERVLET中使用
this.getServlet().getServletContext().getRealPath() // Struts中使用
request.getSession().getServletContext().getRealPath() //Struts和SERVLET都可以使用
一般情况下使用this.getServlet().getServletContext().getRealPath()和request.getSession().getServletContext().getRealPath()是一样的效果,但是当我们部署的一个EAR包里面有多个WAR应用包共享会话时,request.getSession().getServletContext().getRealPath()这个可能取不到你想要的这个应用的绝对路径,而取到的是其他应用的绝对路径,所以,最好还是使用this.getServlet().getServletContext().getRealPath()来获取绝对路径,避免在这种多应用会话共享情况下绝对路径的错乱。

猜你喜欢

转载自shamu.iteye.com/blog/1050665
今日推荐