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中获取项目文件路径的更多相关文章
- 转载:JAVA中获取项目文件路径
本文转载自:http://blog.163.com/michaelgaoit%40126/blog/static/11389538620103711613620/ web 上运行 1:this.get ...
- Java中获取文件路径
Java中获取文件路径 1.实例说明 (1)得到 ClassPath的绝对URI路径 Thread.currentThread().getContextClassLoader().getResourc ...
- java中获取文件路径的几种方式
http://xyzroundo.iteye.com/blog/1116159关于绝对路径和相对路径: 绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz es ...
- java中获取类加载路径和项目根路径的5种方法
import java.io.File; import java.io.IOException; import java.net.URL; public class MyUrlDemo { publi ...
- java中获取类加载路径和项目根路径的5种方式分析
package my; import Java.io.File; import java.io.IOException; import java.net.URL; public class MyUrl ...
- JAVA中获取工程路径的方法
在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProp ...
- Java中获取classpath路径下的资源文件
ClassLoader 提供了两个方法用于从装载的类路径中取得资源: public URL getResource (String name); public InputStream getRes ...
- java 中获取文件路径
方案一: 文件目录如下: 配置文件:firehosetos3sample.properties在src目录下面第一层,与包是一层的 在Getpath_ClassLoader.java类中: Syste ...
- Java 中获取类路径 classpath 的方法
System.out.println("++++++++++++++++++++++++"); String path = System.getProperty("jav ...
随机推荐
- 静态wenb开发,动态web开发
- 11.1Daily Scrum
人员 任务分配完成情况 明天任务分配 王皓南 主网页的框架搭建,任务编号760 研究代码,学习相应语言,讨论设计思路 申开亮 学习数据库的操作,任务编号761 研究代码,学习相应语言,讨论设计思路 王 ...
- HTML+CSS入门
<strong>加粗</strong> <em>斜体</em> <p>段落</p> <span>设置单独样式< ...
- shell dev null 是什么
1:在不想把标准输出和标准出错信息输出到控制台,也不想重定向到文件时经常使用 2:不能忽略其读入功能.从/dev/null读入时都是0 3:系统的垃圾桶,类似于Windows的回收站,不同的是这个设备 ...
- switch..case函数的基础使用一
基本作用:switch中的参数与case的值进行比对,相等则进入case. JDK1.7 switch支持int.Integer.String类型 package com.my.test; impor ...
- android控件---spinner
spinner下拉列表框的列表项有两种配置方式: 1.通过资源文件配置,通过在values种的xml,比如strings.xml中使用<string-array>元素添加制定列表项内容,然 ...
- [spring]启动时报错:NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
Spring V4.1以后的版本在不支持Servlet3.0的应用服务器上跑时会报如下错误: NoSuchMethodError: javax.servlet.http.HttpServletResp ...
- [设计模式] 1/2 工程与抽象工程模式 factory & Abstrac Factory
转载 http://blog.csdn.net/wuzhekai1985 软件领域中的设计模式为开发人员提供了一种使用专家设计经验的有效途径.设计模式中运用了面向对象编程语言的重要特性:封装.继承.多 ...
- WPF Loader
xaml <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml ...
- jstl表达式使用方法
1.jstl.jar servlet.jar支持 2.jsp引入标签库 <%@ taglib prefix="c" uri="http://java.sun.co ...