获取工程根路径方法
request.getSession().getServletContext().getResource("/abc/def.txt")
注意该用方法必须有文件存在才能返回成功
在servlet中
out.println(request.getContextPath());
out.println(request.getRequestURL().toString());
out.println(request.getRequestURI());
out.println(new File("/abc/def.txt").toURI());
out.println(new File("abc/def.txt").toURI());
out.println(request.getRealPath("/abc/def.txt"));
out.println(request.getSession().getServletContext().getRealPath("/abc/def.txt"));
out.println(request.getSession().getServletContext().getResource("/abc/def.txt"));
out.println(this.getClass().getClassLoader().getResource("").toString());
out.println(this.getClass().getClassLoader().getResource("/").toString());
直接部署在tomcat上输出
/test
http://127.0.0.1:8089/test/test2
/test/test2
file:/E:/abc/def.txt
file:/E:/dev/apache-tomcat-5.5.20/bin/abc/def.txt
E:\dev\apache-tomcat-5.5.20\webapps\test\abc\def.txt
E:\dev\apache-tomcat-5.5.20\webapps\test\abc\def.txt
jndi:/localhost/test/abc/def.txt
file:/E:/dev/apache-tomcat-5.5.20/webapps/test/WEB-INF/classes/
file:/E:/dev/apache-tomcat-5.5.20/webapps/test/WEB-INF/classes/
打成war包发布到wesphere
/test
http://172.16.100.75:9080/test/test2
/test/test2
file:/abc/def.txt
file:/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/abc/def.txt
/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/installedApps/svnNode01Cell/test_war.ear/test.war/abc/def.txt
/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/installedApps/svnNode01Cell/test_war.ear/test.war/abc/def.txt
file:/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/installedApps/svnNode01Cell/test_war.ear/test.war/abc/def.txt
file:/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/properties/
bundleresource://42/
打成war包发布到weblogic
/test
http://localhost:7001/test/test2
/test/test2
file:/E:/abc/def.txt
file:/E:/dev/bea/user_projects/domains/mydomain/abc/def.txt
null
null
file:/E:/dev/bea/user_projects/domains/mydomain/servers/AdminServer/tmp/_WL_user/test/t030q4/war/abc/def.txt
file:/E:/dev/bea/user_projects/domains/mydomain/
file:/E:/dev/bea/user_projects/domains/mydomain/servers/SMSServer/tmp/_WL_user/TEST/c8xeoa/war/WEB-INF/classes/