jsp中获取绝对路径和相对路径方法
//在Servlet与JSP中取得当前文件所在的相对路径与绝对路径
JSP中
根目录所对应的绝对路径:request.getRequestURI()
文件的绝对路径 :application.getRealPath(request.getRequestURI());
当前web应用的绝对路径 :application.getRealPath("/");
取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()
3.2 Servlet中获得当前应用的相对路径和绝对路径
根目录所对应的绝对路径:request.getServletPath();
文件的绝对路径 :request.getSession().getServletContext().getRealPath
(request.getRequestURI())
当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8987')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8987>
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/>");
程序
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5425')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5425>String classPath = this.getClass().getClassLoader().getResource("/").getPath();
try {
classPath =URLDecoder.decode(classPath, "gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
例子
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5243')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5243><body>
当前WEB应用的物理路径:<%=application.getRealPath("/")%><BR>
当前你求请的JSP文件的物理路径:<%=application.getRealPath(request.getRequestURI())%><BR>
<%
String path=application.getRealPath(request.getRequestURI());
String dir=new File(path).getParent();
out.println("当前JSP文件所在目录的物理路径" dir "</br>");
String realPath1 = "http://" request.getServerName() ":" request.getServerPort() request.getContextPath() request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/") 1);
out.println("web URL 路径:" realPath1);
%>
</body>
servlet中获得项目绝对路径
String filePath=this.getServletConfig().getServletContext().getRealPath("/");
根目录所对应的绝对路径:request.getServletPath();
文件的绝对路径 :request.getSession().getServletContext().getRealPath(request.getRequestURI())
当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");
ServletContext对象获得几种方式:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4824')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4824>Javax.servlet.http.HttpSession.getServletContext()
Javax.servlet.jsp.PageContext.getServletContext()
Javax.servlet.ServletConfig.getServletContext()
文件的绝对路径 :request.getSession().getServletContext().getRealPath(request.getRequestURI())
当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");
例
//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("/");
您可能感兴趣的文章:
jsp中获取绝对路径和相对路径方法
java中获得当前文件路径多种方法
PHP 获取文件路径(__FILE__的妙用)
jsp文件绝对路径设置方法
file.getPath() getAbsolutePath() getCanonicalPath()区别
通过__FILE__灵活获取文件路径的方法
asp.net将绝对路径转换为虚拟路径的代码举例
Python如何获取当前脚本绝对路径
seo优化绝对路径与相对路径区别
绝对 & 相对URL对搜索排名的影响