JAVA中获取项目文件路径

在java中获得文件的路径在我们做上传文件操作时是不可避免的。

web
上运行 
1:
this.getClass().getClassLoader().getResource("/").getPath(); 
this.getClass().getClassLoader().getResource("").getPath(); 
得到的是
ClassPath的绝对URI路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/

System.getProperty("user.dir");

this.getClass().getClassLoader().getResource(".").getPath();   
得到的是 项目的绝对路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

2:
this.getClass().getResource("/").getPath(); 
this.getClass().getResource("").getPath();
得到的是当前类
文件的URI目录。不包括自己!
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/

this.getClass().getResource(".").getPath();   X 不 能运行

3:
Thread.currentThread().getContextClassLoader().getResource("/").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath() 
得到的是
ClassPath的绝对URI路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/

Thread.currentThread().getContextClassLoader().getResource(".").getPath() 
得到的是 项目的绝对路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

在本地运行中

1:
this.getClass().getClassLoader().getResource("").getPath(); 
this.getClass().getClassLoader().getResource(".").getPath();  
得到的是
ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
this.getClass().getClassLoader().getResource(".").getPath(); 
X 不 能运行

2:
this.getClass().getResource("").getPath(); 
this.getClass().getResource(".").getPath();
得到的是当前类
文件的URI目录。不包括自己!
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/

/D:/myProjects/hp/WebRoot/WEB-INF/classes/    得到的是
ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes

3:
Thread.currentThread().getContextClassLoader().getResource(".").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath()
得到的是 ClassPath的绝对URI路径。。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes

Thread.currentThread().getContextClassLoader().getResource("/").getPath()   
X 不 能运行

最后 
   
在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。
还有request.getContextPath(); 
在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。

JAVA中获取项目文件路径,布布扣,bubuko.com

时间: 2024-08-02 06:51:48

JAVA中获取项目文件路径的相关文章

转载:JAVA中获取项目文件路径

本文转载自:http://blog.163.com/michaelgaoit%40126/blog/static/11389538620103711613620/ web 上运行 1:this.getClass().getClassLoader().getResource("/").getPath(); this.getClass().getClassLoader().getResource("").getPath();  得到的是 ClassPath的绝对URI路

java中获取文件路径的几种方式

http://xyzroundo.iteye.com/blog/1116159关于绝对路径和相对路径: 绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz est.txt 代表了test.txt文件的绝对路径.http://www.sun.com/index.htm也代表了一个URL绝对路径.相对路径:相对与某个基准目录的路径.包含Web的相对路径(HTML中的相对目录),例如:在Servlet中,"/"代表Web应用的跟目录.和物理路径的相对表

JAVA中获取工程路径的方法

转自:http://java-my-life.iteye.com/blog/829504 在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProperty("user.dir")获取你工程的绝对路径. 1.jsp中取得路径: 以工程名为TEST为例 (1)得到包含工程名的当前页面全路径: Java代码   request.getRequestU

java中获取项目路径

代码: package com.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.b

java 中获取文件路径

文件目录如下: 配置文件:firehosetos3sample.properties在src目录下面第一层,与包是一层的 在Getpath_ClassLoader.java类中: System.out.println(Getpath_ClassLoader.class.getResource("/firehosetos3sample.properties")); Getpath_ClassLoader为该类类名,将其替换为src下任一类名也可(可以是不同包) 运行,结果如下:

Java 中获取类路径 classpath 的方法

System.out.println("++++++++++++++++++++++++"); String path = System.getProperty("java.class.path"); String path2 = FreeMarkerWriter.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String path3 = FreeMarkerWriter.c

java中获取路径的几种方式

总是忘记, 备份一下,方便下次用. 第一种: File directory = new File("");//参数为空 String courseFile = directory.getCanonicalPath() ;System.out.println(courseFile); 结果:C:\Documents and Settings\Administrator\workspace\projectName获取当前类的所在工程路径; 第二种: File f = new File(th

JAVA中获取路径

内容来自于snannan_268 关键字: java中获取路径 JAVA中获取路径: 1.jsp中取得路径:   以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.getRequestURI() 结果:/TEST/test.jsp (2)得到工程名:request.getContextPath() 结果:/TEST (3)得到当前页面所在目录下全名称:request.getServletPath() 结果:如果页面在jsp目录下 /TEST/jsp/test.jsp

java中获取系统属性以及环境变量

java中获取系统属性以及环境变量 System.getEnv()和System.getProperties()的差别 从概念上讲,系统属性 和环境变量 都是名称与值之间的映射.两种机制都能用来将用户定义的信息传递给 Java 进程.环境变量产生很多其它的全局效应,由于它们不仅对Java 子进程可见,并且对于定义它们的进程的全部子进程都是可见的.在不同的操作系统上,它们的语义有细微的区别,比方,不区分大写和小写.由于这些原因,环境变量更可能有意料不到的副作用.最好在可能的地方使用系统属性.环境变