当前位置: 移动技术网 > IT编程>开发语言>Java > Java获取*路径实现探讨

Java获取*路径实现探讨

2017年12月12日  | 移动技术网IT编程  | 我要评论
(1)、request.getrealpath("/");//不推荐使用获取工程的根路径
(2)、request.getrealpath(request.getrequesturi());//获取jsp的路径,这个方法比较好用,可以直接在servlet和jsp中使用
(3)、request.getsession().getservletcontext().getrealpath("/");//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用
(4)、 this.getclass().getclassloader().getresource("").getpath();//获取工程classes 下的路径,这个方法可以在任意jsp,servlet,java文件中使用,因为不管是jsp,servlet其实都是java程序,都是一个 class。所以它应该是一个通用的方法。

0、关于绝对路径和相对路径

1.基本概念的理解绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(url和物理路径)例 如:c:xyz est.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm也代表了一个url绝对路径。相对路径:相对与某个基 准目录的路径。包含web的相对路径(html中的相对目录),例如:在servlet中,"/"代表web应用的跟目录。和物理路径的相对表示。例 如:"./" 代表当前目录,"../"代表上级目录。这种类似的表示,也是属于相对路径。另外关于uri,url,urn等内容,请参考rfc相关文档标准。rfc 2396: uniform resource identifiers (uri): generic syntax,(http://www.ietf.org/rfc/rfc2396.txt)2.关于jsp/servlet中的相对路径和绝对路径。 2.1服务器端的地址服务器端的相对地址指的是相对于你的web应用的地址,这个地址是在服务器端解析的(不同于html和javascript中的相对 地址,他们是由客户端浏览器解析的)

1、request.getrealpath

方法:request.getrealpath("/")
得到的路径:c:\program files\apache software foundation\tomcat 5.5\webapps\strutstest\

方法:request.getrealpath(".")
得到的路径:c:\program files\apache software foundation\tomcat 5.5\webapps\strutstest\.

方法:request.getrealpath("")
得到的路径:c:\program files\apache software foundation\tomcat 5.5\webapps\strutstest

request.getrealpath("web.xml")
c:\program files\apache software foundation\tomcat 5.5\webapps\strutstest\web.xml

2、request.getparameter("");
actionform.getmyfile();

方法:string filepath = request.getparameter("myfile");
得到的路径:d:\vss安装目录\users.txt

方法:string filepath = actionform.getmyfile();
得到的路径:d:\vss安装目录\users.txt

--------------------------------------------------
strutstest 为工程名

myfile 在actionform中,为private string myfile;
在jsp页面中:为<html:file property="myfile"></html:file>

--------------------------------------------------

3、获得系统路径

在application中:
system.getproperty("user.dir")

在servlet中:
servletcontext servletcontext = config.getservletcontext();
string rootpath = servletcontext.getrealpath("/");

在jsp中:
application.getrealpath("")

4、其他1

1.可以在servlet的init方法里

string path = getservletcontext().getrealpath("/");

这将获取web项目的全路径

例如 :e:\eclipsem9\workspace\tree\

tree是我web项目的根目录

2.你也可以随时在任意的class里调用

this.getclass().getclassloader().getresource("").getpath();

这将获取 到classes目录的全路径

例如 : /d:/workspace/strutstest/webroot/web-inf/classes/

还有 this.getclass().getresource("").getpath().tostring();

这将获取 到 /d:/workspace/strutstest/webroot/web-inf/classes/bl/

这个方法也可以不在web环境里确定路径,比较好用

3.request.getcontextpath();

获得web根的上下文环境

如 /tree

tree是我的web项目的root context

5、其他2

java获取路径几种途径- -
1. jdk如何判断程序中的路径呢?一般在编程中,文件路径分为相对路径和绝对路径,绝对路径是比较好处理的,但是不灵活,因此我们在编程中对文件进行操作的时候,一般都是读取文件的相对路径,

相对路径可能会复杂一点,但是也是比较简单的,相对的路径,主要是相对于谁,可以是类加载器的路径,或者是当前 java文件下的路径,在jsp编程中可能是相对于站点的路径,相对于站点的路径,我们可以通过 getservletcontext().getrealpath("\") 和request.getrealpath("\"):这个是取得站点的绝对路径; 而getcontextpath():取得站点的虚拟路径;

2. class.getclassloader.getpath():取得类加载器的路径:什么是类加载器呢? 一般类加载器有系统的和用户自己定义的;系统的classloader就是jdk提供的,他的路径就是jdk下的路径,或者在 jsp编程,比如tomcat ,取得的类加载器的位置就是tomaca自己设计的加载器的路径,

明白了这些之后,对于文件路径的操作就会相当的清楚,我们在编程的时候,只要想清楚我们所操作的文件是相对于什么路径下的,取得相对路径就可以了.

6、总结

1、获取web服务器下的文件路径
request.getrealpath("/")
application.getrealpath("")【jsp中 】
servletcontext().getrealpath("")

system.getproperty("user.dir")【不同位置调用,获取的路径是动态变化的】

2、获取本地路径

jsp中,<html:file property="myfile"/>

request.getparameter("myfile");
actionform.getmyfile();
获取的值相同:如d:\vss安装目录\users.txt

*********************************

this.getclass().getclassloader().getresource("").getpath();
==/d:/workspace/strutstest/webroot/web-inf/classes/
this.getclass().getresource("").getpath().tostring();
==/d:/workspace/strutstest/webroot/web-inf/classes/bl/

3、获取相对路径

request.getcontextpath();

如:/strutstest

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网