一、取得Web中的路径
①包含工程名的当前页面全路径 request.getRequestURI()
/TEST/test.jsp
②工程名: request.getContextPath()
/TEST
③当前页面所在目录下全名称 request.getServletPath()
若页面在jsp目录下/TEST/jsp/test.jsp
④页面所在服务器的全路径 request.getSession().getServletContext().getRealPath("a.jsp");
D:\resin\webapps\TEST\test.jsp
若想定位到某个文件夹则 getServletContext().getRealPath("WEB-INF");
getServletContext()其实取得的是application
⑤页面所在服务器的绝对路径: request.getSession().getServletContext().(request.getRequestURI()).getParent();
D:\resin\webapps\TEST
例1:
一般使用我们只要设置工程名就可以了如
6.String path = request.getContextPath()+"/"; request.setAttribute( "path", path);
/TEST/
例2
System.getProperty("user.dir")
E:\gitrepository\report
例3
在类路径下取得
UserController.class.getClass().getResource("/").getPath()
/D:/WorkSpace/SpringMVC2/build/classes/
例4
new File("1.jpg");
这只文件表示在当前的工程目录下