## java获取项目绝对路径
String realPath = this.getRequest().getSession().getServletContext().getRealPath("/").replaceAll("\\\\", "/");
String file = realPath+"WEB-INF/imgFile/file/excel/"+newFileName;
File uploadPic = new File(file);
if(!uploadPic.exists()){
uploadPic.mkdirs();
}
request.getSession().getServletContext() 获取的是Servlet容器对象,相当于tomcat容器了。getRealPath("/") 获取实际路径,“/”指代项目根目录,所以代码返回的是项目在容器中的实际发布运行的根路径
request.getSession()与this.getServletContext()有什么区别
request.getSession()是得到当前会话session,this.getServletContext()得到此全局的变量,其中包含很多的session
java获取项目绝对路径
最新推荐文章于 2022-10-14 10:23:11 发布