javaweb获取项目目录的几种方法

来源:互联网 发布:电气控制柜设计软件 编辑:程序博客网 时间:2024/06/05 18:48

1、在开发的项目中,需要上传下载文件,此时需要获取文件位置

2、通过HttpServletRequest获取

request.getSession().getServletContext().getRealPath("/")

例如获取的路径为:D:\impay\apache-tomcat-7.0.65\wtpwebapps\oleka_service\

3、通过class反射机制获取

Test.class.getClassLoader().getResource("").getPath(),其中Test为本代码所在类名

例如获取的路径为:D:\impay\apache-tomcat-7.0.65\wtpwebapps\oleka_service\WEB-INF\classes