本地文件的路径:
当前操作文件 c:/folderA/fileA
1,本地文件的绝对路径:
d:/folderB/fileB 指向另一磁盘的文件
2,相对当前文件夹根目录(c:)的路径:
/floderB/fileB 指向c:/folderB/fileB
3,相对当前文件夹(c:/folderA/)的路径:
floderB/fileB 指向c:/folderA/folderB/fileB
获取当前路径:
1,System.getProperty("user.dir")获取项目根路径:"
D:\Workspaces\project"
2,ClassName
.class.getResource("")获得类所在路径:"file:/D:/Workspaces/project/bin/pakage/"
3,ClassName.class.getResource("/")获得类所在路径:"file:/D:/Workspaces/project/bin/"
4,
File file = new File("");
file.getCanonicalPath();获得项目根路径:"D:\Workspaces\project"
网络资源的路径:
当前URL:
http:(网络协议)//hostA.domainA:80(主机名:端口)/projectA(项目名)/folderA(文件夹)/fileA
1,绝对URL:
http://hostB.domainB:80/projectB/folderB/fileB 指向另一个网站的资源
2,相对当前文件夹根路径 (http://hostA.domainA:80) 的URL:
/projectB/folderB/fileB 指向 http://hostA.domainA:80/projectB/folderB/fileB
3,相对当前文件夹路径(http://hostA.domainA:80/projectA/folderA/)的URL:
floderB/fileB 指向 指向 http://hostA.domainA:80/projectA/folderA/folderB/fileB
获取路径:
1,
request.getContextPath()获得 "/project"
2,application.getContextPath() 获得 "/project"
3,
request.getRequestURL()获取当前文件URL绝对路径:"http://host:8080/project/folder/file"
4,
request.getRequestURI()获取当前文件URI: "/project/floder/file"
5,
request.getServletPath()获得当前文件相对项目根路径的相对路径 "/floder/file
"
6,application.getRealPath("file")获得指定文件的本地绝对路径"D:\tomcat\webapps\project\folder\file "
7,
application.getRealPath("")获得项目文件夹的本地绝对路径 "D:\tomcat\webapps\project\ "
8,new File(application.getRealPath("file"))
.getParent()获得指定文件的文件夹的本地绝对路径:"D:\tomcat\webapps\project\folder"
9,
System.getProperty("user.dir")获得tomcat路径:"D:\tomcat\bin"
10,ServletName.class.getResource("")获得类所在路径:"file:/D:/tomcat/webapps/project/WEB-INF/classes/package/"
11,ServletName.class.getResource("/")获得类所在路径:"file:/D:/tomcat/webapps/project/WEB-INF/classes/"