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:\myecllipse\apache-tomcat-7.0.77-windows-x64\apache-tomcat-7.0.77\wtpwebapps\hcwproject\image
最后一个路径其实是工程在那个目录运行路径,不该路径默认会在工作空间