java关于用request获取路径

System.out.println("getContextPath:"+req.getContextPath());
System.out.println("getServletPath:"+req.getServletPath());
System.out.println("getRequestURI:"+req.getRequestURI());
System.out.println("getRequestURL:"+req.getRequestURL());
System.out.println("getRealPath:"+req.getSession().getServletContext().getRealPath("image") );

输出结果为

getContextPath:/maven
getServletPath:/upload.do
getRequestURI:/maven/upload.do
getRequestURL:http://localhost:8080/maven/upload.do
getRealPath:D:myecllipseapache-tomcat-7.0.77-windows-x64apache-tomcat-7.0.77wtpwebappshcwprojectimage

最后一个路径其实是工程在那个目录运行路径,不该路径默认会在工作空间

原文地址:https://www.cnblogs.com/itzyz/p/12262266.html