java/jsp获取当前路径的几种方法

本文结合个人实验与网上资料整合@http://blog.csdn.net/snannan_268/article/details/5511614


String path = new File(application.getRealPath(request.getServletPath() )).getParent();   //路径获取jsp文件绝对路径


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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值