方法一:
<span style="font-size:14px;">package com.waxberry.common.util;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
public class MyLocalDemo {
public static void main(String[] args) {
MyLocalDemo myLocalDemo = new MyLocalDemo();
System.out.println("path=="+myLocalDemo.getWebInfPath());
}
private String getWebInfPath(){
URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
String path = url.toString();
int index = path.indexOf("WEB-INF");
if(index == -1){
index = path.indexOf("classes");
}
if(index == -1){
index = path.indexOf("bin");
}
path = path.substring(0, index);
if(path.startsWith("zip")){//当class文件在war中时,此时返回zip:D:/...这样的路径
path = path.substring(4);
}else if(path.startsWith("file")){//当class文件在class文件中时,此时返回file:/D:/...这样的路径
path = path.substring(6);
}else if(path.startsWith("jar")){//当class文件在jar文件里面时,此时返回jar:file:/D:/...这样的路径
path = path.substring(10);
}
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return path;
}
}
</span>
输出:
<span style="font-size:14px;">path==E:/tl/java_workspaces/waxberry/target/</span>
方法二“:
<span style="font-size:14px;">package com.waxberry.common.util;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
public class MyLocalDemo {
public static void main(String[] args) {
MyLocalDemo myLocalDemo = new MyLocalDemo();
System.out.println("path=="+myLocalDemo.getXmlPath());
}
/**
* 获取WEB-INF目录下面server.xml文件的路径
* @return
*/
public String getXmlPath()
{
//file:/D:/JavaWeb/.metadata/.me_tcat/webapps/TestBeanUtils/WEB-INF/classes/
String path=Thread.currentThread().getContextClassLoader().getResource("").toString();
path=path.replace('/', '\\'); // 将/换成\
path=path.replace("file:", ""); //去掉file:
path=path.replace("classes\\", ""); //去掉class\
path=path.substring(1); //去掉第一个\,如 \D:\JavaWeb...
path+="web.xml";
//System.out.println(path);
return path;
}
}
</span>
输出:
<span style="font-size:14px;">path==E:\tl\java_workspaces\waxberry\target\web.xml</span>
备注:
<span style="font-size:14px;">我们通过以下语句获取class目录所在的目录:
Thread.currentThread().getContextClassLoader().getResource("").toString();
一般获取到的结果形式为:
file:/D:/JavaWeb/.metadata/.me_tcat/webapps/TestBeanUtils/WEB-INF/classes/
根据以上这个字符串,即可提取出WEB-INF目录的绝对路径吧!</span>
方法三:
servelt环境
<span style="font-size:14px;"> String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");</span>
<span style="font-size:14px;">String tempFile = req.getSession().getServletContext().getRealPath("/tempUpload")+"/";</span>
补充:
另:在Jsp,Servlet,Java中详细获得路径的方法!
1.jsp中取得路径:
以工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:request.getRequestURI()
结果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:request.getServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
(4)得到页面所在服务器的全路径:application.getRealPath(“页面.jsp”)
结果:D:\resin\webapps\TEST\test.jsp
(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:\resin\webapps\TEST
2.在类中取得路径:
(1)类的绝对路径:Class.class.getClass().getResource(“/”).getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路径:System.getProperty(“user.dir”)
结果:D:\TEST
3.在Servlet中取得路径:
(1)得到工程目录:request.getSession().getServletContext().getRealPath(“”) 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/tes
方法四:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<img src="<%=basePath%>+"signimg/"+文件名>