1、取得Web中的路徑
①包含工程名的當前頁面全路徑 request.getRequestURI()
/TEST/test.jsp
②工程名: request.getContextPath()git
/TESTweb
③當前頁面所在目錄下全名稱 request.getServletPath()服務器
若頁面在jsp目錄下/TEST/jsp/test.jspapp
④頁面所在服務器的全路徑 request.getSession().getServletContext().getRealPath("a.jsp");
D:\resin\webapps\TEST\test.jsp
若想定位到某個文件夾則 getServletContext().getRealPath("WEB-INF");
getServletContext()其實取得的是applicationwebapp
⑤頁面所在服務器的絕對路徑: request.getSession().getServletContext().(request.getRequestURI()).getParent();
D:\resin\webapps\TEST
例1:
通常使用咱們只要設置工程名就能夠了如jsp
6.String path = request.getContextPath()+"/"; request.setAttribute( "path", path);
/TEST/ui
例2
System.getProperty("user.dir") get
E:\gitrepository\report
例3
在類路徑下取得it
UserController.class.getClass().getResource("/").getPath()io
/D:/WorkSpace/SpringMVC2/build/classes/
例4
new File("1.jpg"); 這隻文件表示在當前的工程目錄下