本文结合个人实验与网上资料整合@http://blog.csdn.net/snannan_268/article/details/5511614
String path = new File(application.getRealPath(request.getServletPath() )).getParent(); //路径获取jsp文件绝对路径
--ROOT/player.jsp
<%@page language="java" contentType="text/html;charset=utf-8"%>
<%@page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@page import="java.io.*"%>
<%
String path = new File(application.getRealPath(request.getServletPath() )).getParent(); //路径
out.println(path+" "+request.getServletPath()+"<br/>" );
out.println(System.getProperty("user.dir")+"<br/>");
out.println("-------------------------<br/>");
File directory = new File("");//设定为当前文件夹
out.println(directory.getCanonicalPath()+"<br/>");//获取标准的路径
out.println(directory.getAbsolutePath()+"<br/>");//获取绝对路径
out.println("-------------------------<br/>");
directory = new File("abc");
out.println(directory.getCanonicalPath()+"<br/>");//获取标准的路径
out.println(directory.getAbsolutePath()+"<br/>");//获取绝对路径
out.println(directory.getPath()+"<br/>");//获取标准的路径
out.println("-------------------------<br/>");
directory = new File(".");
out.println(directory.getCanonicalPath()+"<br/>");//获取标准的路径
out.println(directory.getAbsolutePath()+"<br/>");//获取绝对路径
out.println(directory.getPath()+"<br/>");//获取标准的路径
out.println("-------------------------<br/>");
directory = new File("..");
out.println(directory.getCanonicalPath()+"<br/>");//获取标准的路径
out.println(directory.getAbsolutePath()+"<br/>");//获取绝对路径
out.println(directory.getPath()+"<br/>");//获取标准的路径
out.println("-------------------------<br/>");//<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">.jsp中取得路径:</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">3.在Servlet中取得路径: </span>
out.println(request.getRequestURI() +"<br/>");
out.println(request.getContextPath() +"<br/>");
out.println(request.getServletPath() +"<br/>");
out.println(application.getRealPath("player.jsp") +"<br/>");
out.println(new File(application.getRealPath(request.getRequestURI())).getParent() +"<br/>");
out.println(request.getSession().getServletContext().getRealPath("") +"<br/>");<span style="font-family: Arial;">//3.在Servlet中取得路径: </span>
out.println(request.getRequestURL() +"<br/>");
%>
结果如下
F:\WebSpace\J2EE\web\webapps\ROOT /player.jsp
C:\Users\FuChenD
-------------------------
C:\Users\FuChenD
C:\Users\FuChenD
-------------------------
C:\Users\FuChenD\abc
C:\Users\FuChenD\abc
abc
-------------------------
C:\Users\FuChenD
C:\Users\FuChenD\.
.
-------------------------
C:\Users
C:\Users\FuChenD\..
..
-------------------------
/player.jsp
/player.jsp
null
F:\WebSpace\J2EE\web\webapps\ROOT
F:\WebSpace\J2EE\web\webapps\ROOT\
http://www.lovedxy.com:8080/player.jsp
--G:/Media/player.jsp
</pre>结果如下<p></p><p></p><pre name="code" class="html">G:\Media /player.jsp
C:\Users\FuChenD
-------------------------
C:\Users\FuChenD
C:\Users\FuChenD
-------------------------
C:\Users\FuChenD\abc
C:\Users\FuChenD\abc
abc
-------------------------
C:\Users\FuChenD
C:\Users\FuChenD\.
.
-------------------------
C:\Users
C:\Users\FuChenD\..
..
-------------------------
/Media/player.jsp
/Media
/player.jsp
null
G:\Media\Media
G:\Media\
http://www.lovedxy.com:8080/Media/player.jsp
--G:/Media/四大名著之三国演义/player.jsp
</pre>结果如下<p></p><p></p><pre name="code" class="html">G:\Media\四大名著之三国演义 /四大名著之三国演义/player.jsp
C:\Users\FuChenD
-------------------------
C:\Users\FuChenD
C:\Users\FuChenD
-------------------------
C:\Users\FuChenD\abc
C:\Users\FuChenD\abc
abc
-------------------------
C:\Users\FuChenD
C:\Users\FuChenD\.
.
-------------------------
C:\Users
C:\Users\FuChenD\..
..
-------------------------
/Media/%E5%9B%9B%E5%A4%A7%E5%90%8D%E8%91%97%E4%B9%8B%E4%B8%89%E5%9B%BD%E6%BC%94%E4%B9%89/player.jsp
/Media
/四大名著之三国演义/player.jsp
null
G:\Media\Media\%E5%9B%9B%E5%A4%A7%E5%90%8D%E8%91%97%E4%B9%8B%E4%B8%89%E5%9B%BD%E6%BC%94%E4%B9%89
G:\Media\
http://www.lovedxy.com:8080/Media/%E5%9B%9B%E5%A4%A7%E5%90%8D%E8%91%97%E4%B9%8B%E4%B8%89%E5%9B%BD%E6%BC%94%E4%B9%89/player.jsp