【转】Java获取路径方法
帖一:http://shijian.iteye.com/blog/208190
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.可以在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根的上下文环境如 /treetree是我的web项目的root context
?
5、其他12
java获取路径几种途径- -
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
?
?
?
帖二:http://teamojiao.iteye.com/blog/446615
关于绝对路径和相对路径:?
绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(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中的相对地址,他们是由客户端浏览器解析的)?
第一种:?
File f = new File(this.getClass().getResource("/").getPath());?
System.out.println(f);?
结果:?
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin?
获取当前类的所在工程路径;?
如果不加“/”?
File f = new File(this.getClass().getResource("").getPath());?
System.out.println(f);?
结果:?
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test?
获取当前类的绝对路径;?
第二种:?
File directory = new File("");//参数为空?
String courseFile = directory.getCanonicalPath() ;?
System.out.println(courseFile);?
结果:?
C:\Documents and Settings\Administrator\workspace\projectName?
获取当前类的所在工程路径;?
File.getCanonicalPath()和File.getAbsolutePath()大约只是对于new File(".")和new File("..")两种路径有所区别。
# 对于getCanonicalPath()函数,“."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹
# 对于getAbsolutePath()函数,则不管”.”、“..”,返回当前的路径加上你在new File()时设定的路径
# 至于getPath()函数,得到的只是你在new File()时设定的路径
比如当前的路径为 C:\test :
File directory = new File("abc");
directory.getCanonicalPath(); //得到的是C:\test\abc
directory.getAbsolutePath();??? //得到的是C:\test\abc
direcotry.getPath();??????????????????? //得到的是abc
File directory = new File(".");
directory.getCanonicalPath(); //得到的是C:\test
directory.getAbsolutePath();??? //得到的是C:\test\.
direcotry.getPath();??????????????????? //得到的是.
File directory = new File("..");
directory.getCanonicalPath(); //得到的是C:\
directory.getAbsolutePath();??? //得到的是C:\test\..
direcotry.getPath();??????????????????? //得到的是..
?
第三种:?
URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");?
System.out.println(xmlpath);?
结果:?
file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt?
获取当前工程src目录下selected.txt文件的路径?
第四种:?
System.out.println(System.getProperty("user.dir"));?
结果:?
C:\Documents and Settings\Administrator\workspace\projectName?
获取当前工程路径?
第五种:?
System.out.println( System.getProperty("java.class.path"));?
结果:?
C:\Documents and Settings\Administrator\workspace\projectName\bin?
获取当前工程路径
另外:System.getProperty()中的字符串参数如下:
System.getProperty()参数大全
# java.version??????????????????????????????? Java Runtime Environment version?
# java.vendor??????????????????????????????? Java Runtime Environment vendor?
# java.vendor.url?????????????????????????? Java vendor URL?
# java.home??????????????????????????????? Java installation directory?
# java.vm.specification.version?? Java Virtual Machine specification version?
# java.vm.specification.vendor??? Java Virtual Machine specification vendor?
# java.vm.specification.name????? Java Virtual Machine specification name?
# java.vm.version??????????????????????? Java Virtual Machine implementation version?
# java.vm.vendor??????????????????????? Java Virtual Machine implementation vendor?
# java.vm.name??????????????????????? Java Virtual Machine implementation name?
# java.specification.version??????? Java Runtime Environment specification version?
# java.specification.vendor???????? Java Runtime Environment specification vendor?
# java.specification.name?????????? Java Runtime Environment specification name?
# java.class.version??????????????????? Java class format version number?
# java.class.path????????????????????? Java class path?
# java.library.path???????????????? List of paths to search when loading libraries?
# java.io.tmpdir?????????????????????? Default temp file path?
# java.compiler?????????????????????? Name of JIT compiler to use?
# java.ext.dirs?????????????????????? Path of extension directory or directories?
# os.name????????????????????????????? Operating system name?
# os.arch????????????????????????????????? Operating system architecture?
# os.version?????????????????????? Operating system version?
# file.separator???????????????????????? File separator ("/" on UNIX)?
# path.separator????????????????? Path separator (":" on UNIX)?
# line.separator?????????????????????? Line separator ("\n" on UNIX)?
# user.name??????????????????????? User's account name?
# user.home????????????????????????????? User's home directory?
# user.dir?????????????????????????????? User's current working directory
?
第六种:
tomcat下获得项目的绝对路径private String projectName="sz_pro";????????//? 你项目的名称
//获取当前项目的绝对路径?
?
?
转自:http://java-admin.iteye.com/blog/519622