java中获取项目在tomcat目录下的路径方法
HttpServletRequest request
//获取的是ROOT项目在tomcat下的路径
方法1:
String path = request.getSession().getServletContext().getRealPath("/");
path值如下:
C:\develop\apache-tomcat-7.0.75\webapps\ROOT\
方法2:
String path=System.getProperty("catalina.home");
path值如下:
C:\develop\apache-tomcat-7.0.75
/**
* 根据图片路径,判断图片是否为空,如果不为空则看是否存在硬盘中,如果不存在则给默认图片,否则显示出该图片
*/
String photo="/upload/noimg.png";
String headSculpturePath = request.getSession().getServletContext().getRealPath("/upload")+"/"; //如:C:\\develop\\apache-tomcat-7.0.75\\webapps\\ROOT\\upload
String serverPath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();//如:http://localhost:9099
if(!"".equals(torgcadre.getPhoto())&&torgcadre.getPhoto()!=null){
File file = new File(headSculpturePath+torgcadre.getPhoto());
if(!file.exists()){
torgcadre.setPhoto(serverPath+photo);
}else{
torgcadre.setPhoto(serverPath+"/upload/"+torgcadre.getPhoto());
}
}else{
torgcadre.setPhoto(serverPath+photo);
}