Tomcat部署路径选择Use workspace metadata (does not modify Tomcat installation)有效。
import java.io.File;
import javax.servlet.http.HttpServletRequest;
/**
* 获取本地项目的一些路径
* @author lisi
*
*/
public class GWPWpath {
private static File f = null;
public GWPWpath() {
super();
//获取类加载的项目根路径,一般位于开发工具工作空间目录下
f=new File(this.getClass().getResource("/../../").getPath());
}
/**
* 获取eclipse开发工具工作空间路径
* @return
*/
public String getWokespacePath(){
//根据eclipse工作空间的文件夹名.metadata获取eclipse开发工具工作空间路径
String wokespacepath=f.toString().substring(0, f.toString().indexOf(".metadata"));
return wokespacepath;
}
/**
* web项目可用
* 获取项目相对路径
* @param req 请求对象
* @return
*/
public String getProjectRelativePath(HttpServletRequest req){
//获取项目相对路径
String projectpath=req.getSession().getServletContext().getContextPath();
return projectpath;
}
/**
* 通用
* 获取项目名
* @return
*/
public String get