//在Servlet與JSP中取得當前文件所在的相對路徑與絕對路徑
//JSP中
out.println("根目錄所對應的絕對路徑:" request.getRequestURI() "<br/>");
String strPathFile = application.getRealPath(request.getRequestURI());
out.println("文件的絕對路徑:" strPathFile "<br/>");
out.println(application.getRealPath(request.getRequestURI()));
String strDirPath = new File(application.getRealPath(request.getRequestURI())).getParent();
out.println("目錄的絕對路徑:" strDirPath "<br/>");
//Servlet中
//JSP中的application對象就是Servlet中的ServerContext,所以在Servlet中是如此獲得
//import java.io.File;
System.out.println("根目錄所對應的絕對路徑:" request.getServletPath() "<br/>");
String strPathFile = request.getSession().getServletContext().getRealPath(request.getRequestURI());
System.out.println("文件的絕對路徑:" strPathFile "<br/>");
String strDirPath = new File(request.getSession().getServletContext().getRealPath(request.getRequestURI())).getParent();
System.out.println("目錄的絕對路徑:" strDirPath "<br/>");
文件名不能包括以下字符:/:*?"<>|
獲取Web項目的全路徑
String strDirPath = getServletContext().getRealPath("/");