[url]http://blog.youkuaiyun.com/yaerfeng/article/details/7297479[/url]
在jsp和class文件中调用的相对路径不同。 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProperty("user.dir")获取你工程的绝对路径。
另:在Jsp,Servlet,Java中详细获得路径的方法!
[color=red][b]1.jsp中取得路径[/b][/color]:
以工程名为TEST为例:
(1)[color=blue][b]得到包含工程名的当前页面全路径[/b][/color]:request.getRequestURI()
结果:/TEST/test.jsp
(2)[color=blue][b]得到工程名[/b][/color]:request.getContextPath()
结果:/TEST
(3)[color=blue][b]得到当前页面所在目录下全名称[/b][/color]:request.getServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
(4)[color=blue][b]得到页面所在服务器的全路径[/b][/color]:application.getRealPath("页面.jsp")
结果:D:/resin/webapps/TEST/test.jsp
(5)[color=darkblue][b]得到页面所在服务器的绝对路径[/b][/color]:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:/resin/webapps/TEST
[color=red][b]2.在类中取得路径[/b][/color]:
(1)[color=blue][b]类的绝对路径[/b][/color]:String u=Class.class.getClass().getResource("/").getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)[color=blue][b]得到工程的路径[/b][/color]:System.getProperty("user.dir")
结果:D:/TEST
[color=red][b]3.在Servlet中取得路径[/b][/color]:
(1)得[color=blue][b]到工程目录[/b][/color]:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:/Tomcat/webapps/TEST
(2)[color=blue][b]得到IE地址栏地址[/b][/color]:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)[color=blue][b]得到相对地址[/b][/color]:request.getRequestURI()
结果:/TEST/test
在jsp和class文件中调用的相对路径不同。 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProperty("user.dir")获取你工程的绝对路径。
另:在Jsp,Servlet,Java中详细获得路径的方法!
[color=red][b]1.jsp中取得路径[/b][/color]:
以工程名为TEST为例:
(1)[color=blue][b]得到包含工程名的当前页面全路径[/b][/color]:request.getRequestURI()
结果:/TEST/test.jsp
(2)[color=blue][b]得到工程名[/b][/color]:request.getContextPath()
结果:/TEST
(3)[color=blue][b]得到当前页面所在目录下全名称[/b][/color]:request.getServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
(4)[color=blue][b]得到页面所在服务器的全路径[/b][/color]:application.getRealPath("页面.jsp")
结果:D:/resin/webapps/TEST/test.jsp
(5)[color=darkblue][b]得到页面所在服务器的绝对路径[/b][/color]:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:/resin/webapps/TEST
[color=red][b]2.在类中取得路径[/b][/color]:
(1)[color=blue][b]类的绝对路径[/b][/color]:String u=Class.class.getClass().getResource("/").getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
//str会得到这个函数所在类的路径
String str = u.toString();
//截去一些前面6个无用的字符
str=str.substring(6,str.length());
//将%20换成空格(如果文件夹的名称带有空格的话,会在取得的字符串上变成%20)
str=str.replaceAll("%20", " ");
//查找“WEB-INF”在该字符串的位置
int num = str.indexOf("WEB-INF");
//截取即可
str=str.substring(0, num+"WEB-INF".length());
(2)[color=blue][b]得到工程的路径[/b][/color]:System.getProperty("user.dir")
结果:D:/TEST
[color=red][b]3.在Servlet中取得路径[/b][/color]:
(1)得[color=blue][b]到工程目录[/b][/color]:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:/Tomcat/webapps/TEST
(2)[color=blue][b]得到IE地址栏地址[/b][/color]:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)[color=blue][b]得到相对地址[/b][/color]:request.getRequestURI()
结果:/TEST/test