日期:2014-05-16  浏览次数:20669 次

windows linux 下,获取java项目绝对路径的问题

?

String realPath = request.getSession().getServletContext().getRealPath("/") ;

在windows中结尾带\,

在linux中结尾不带/

笨方法,在后面再加一个/,windows也认,linux也正确

String realPath = request.getSession().getServletContext().getRealPath("/") + "/";

?

也可以判断处理

//windows下
if("\\".equals(File.separator)){???
????……?

}
//linux下
if("/".equals(File.separator)){???
????……?

}

?

可以把这个逻辑加入到

ServletContex监听器,项目启动时放到系统属性里,System.setProperty("",""); 用时再取

J2EE监听器和过滤器基础

http://happyqing.iteye.com/blog/1899756

?