相对路径与绝对路径

本地文件的路径:

当前操作文件 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/"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值