源代码转自:http://topic.csdn.net/u/20070511/02/a7b11a71-ed08-4a3d-99b1-92d6933f3ca7.html
import java.net.URL;
/**
* Java通用的绝对路径获取终极解决方案
* 获取classes,WEB-INF等的绝对路径
* @测试系统:
* Windows Linux Unix
* @测试环境:
* Tomcat6.0.10
* Resin3.1.1
* Jboss4.0.0/5.0.0
* WebLogic9.1
* WebSphere6.1
* WasCE1.1
* Apusic4.0.3
* JFox3
* Jetty6.1.3
*
* @author Jxva
* @support Jxva.Com
*
*/
public class JPath
{
public static void main(String args[])
{
JPath j=new JPath();
System.out.println(getClassPath());
System.out.println(getWebinfoPath());
System.out.println(getJxvaHome());
System.out.println(getAppPath());
System.out.println(getRootPath());
System.out.println(getJxvaFramework());
System.out.println(j.getRealPath());
}
/**
*
* @return ~/classes/
*/
public static String getClassPath(){
JPath j=new JPath();
String t=j.getRealPath();
t=t.replaceAll( "file:/ ", " "); //windows
t=t.replaceAll( "file: ", " "); //linux,unix
t=t.replaceAll( "wsjar: ", " "); //websphere wsjar: has to at jar: before
t=t.replaceAll( "jar: ", " "); //tomcat,jboss,resin,wasce,apusic
t=t.replaceAll( "zip: ", " "); //weblogic
t=t.replaceAll( "/./ ", "/ "); //weblogic
//if this class be included .jar file,will replace "/lib/*.!/ " to "/classes/ "
t=t.replaceAll( "/lib/([^/ ' ']+)!/ ", "/classes/ "); //jar
t=t.split( "/classes/ ")[0]+ "/classes/ ";
return t;
}
/**
* if this path include "WEB-INF " will return "WEB-INF " 's absolute path
* @return ~/WEB-INF/
*/
public static String getWebinfoPath(){
//remove string "classes/ "
return getClassPath().substring(0,getClassPath().length()-8);
}
/**
*
* @return ~/JxvaHome/
*/
public static String getJxvaHome(){
return getWebinfoPath()+ "JxvaHome/ ";
}
/**
*
* @return application 's path
*/
public static String getAppPath(){
String[] s=getWebinfoPath().split( "/ ");
StringBuffer sb=new StringBuffer();
for(int i=0;i <s.length-1;i++){
sb.append(s[i]+ "/ ");
}
return sb.toString();
}
/**
*
* @return root 's path
*/
public static String getRootPath(){
return getClassPath().split( "/ ")[0]+ "/ ";
}
/**
* JxvaFramework框架的公共目录,确保对根目录有可写权限,一般虚拟主机将无法使用
* @return JxvaFrameowrk
*/
public static String getJxvaFramework(){
return getRootPath()+ "JxvaFramework/ ";
}
private String getRealPath()
{
String strClassName = getClass().getName();
String strPackageName = " ";
if (getClass().getPackage() != null) strPackageName = getClass().getPackage().getName();
String strClassFileName = " ";
if (! " ".equals(strPackageName))
strClassFileName = strClassName.substring(strPackageName.length() + 1, strClassName.length());
else
strClassFileName = strClassName;
URL url = getClass().getResource(strClassFileName + ".class ");
String strURL = url.toString();
strURL = strURL.replaceAll( "%20 ", " ");
return strURL;
}
}
以下是我修改的方法,满足我的需要
import java.net.URL;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
public class JasperPath {
/**
* 返回.Jasper文件的路径
*
* @return .Jasper文件的路径
*/
public static String getJasperPath() {
JasperPath j = new JasperPath();
String t = j.getRealPath();
t = t.replaceAll("file:/","");
t = t.replaceAll("/JasperPath.class","");
return t;
}
private String getRealPath() {
String strClassName = getClass().getName();
String strPackageName = " ";
if (getClass().getPackage() != null)
strPackageName = getClass().getPackage().getName();
String strClassFileName = " ";
if (!" ".equals(strPackageName))
strClassFileName = strClassName.substring(
strPackageName.length() + 1, strClassName.length());
else
strClassFileName = strClassName;
URL url = getClass().getResource(strClassFileName + ".class ");
String strURL = url.toString();
strURL = strURL.replaceAll("%20", " ");
return strURL;
}
}
分享到:
相关推荐
windows linux 下,获取java项目绝对路径的方法,struts2设置了struts.multipart.saveDir后会在根目录建立文件夹,这样会涉及linux下的权限问题
java project和java web project项目中如何获得相对路径、绝对路径问题详解
java获取路径几种途径,1、获取web服务器下的文件路径,2、获取本地路径,3、获取相对路径
JSP中获得当前应用的相对路径和绝对路径 根目录所对应的绝对路径、 文件的绝对路径 、 当前web应用的绝对路径、 取得请求文件的上层目录……
JSP中java脚本获取绝对路径 。
主要介绍了JAVA获取文件绝对路径的方法,涉及针对文件路径的操作技巧,需要的朋友可以参考下
一般在eclipse环境下使用,通过java类可以获取此类的存在路径
java web中File的相对路径与绝对路径总结,用于在运行时获取配置文件、动态加载对象等
Java中获取文件路径的几种方式。 关于绝对路径和相对路径
java中 tomcat服务器的根路径、jsp的根路径、类路径如何获取一直困扰着很多编程人员,看了它也许你会清晰很多。
基本概念的理解绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz est.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm也代表了一个URL绝对路径。相对...
3. 在 JSP/Servlet 中获得当前应用的相对路径和绝对路径 在 JSP 中,可以使用 request.getRequestURI() 获取当前文件的绝对路径,然后使用 application.getRealPath(request.getRequestURI()) 获取当前文件的绝对...
开发web工程时经常要获取工程的根目录,自己用Java实现的获取Tomcat下war包部署的Web工程根目录路径的方法,主要利用web工程默认的目录结构,此外也可以指定工程名称获取工程目录的绝对路径
相对路径可以是相对于当前类所在的包的路径,也可以是绝对路径。如果路径以 "/" 开头,则表示绝对路径,否则表示相对路径。 例如,com.explorers.Test.class.getResourceAsStream("abc.jpg") 和 ...
主要介绍了java区分绝对路径和相对路径的方法,实例分析了java针对路径操作的相关技巧,需要的朋友可以参考下
主要为大家详细介绍了java获取各种路径的基本方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
如何在java中获取文件的绝对路径,以及在java web开发中得到文件的绝对路径。
Java 开发中我们经常要获取文件的路径,比如读取配置文件等等。今天我们就关于文件的路径和如何读取文件简单地探讨一下。 2. 文件的路径 文件的路径通常有 相对路径 与 绝对路径。 2.1 相对路径 以当前文件为基准...
尽量使用URI形式的绝对路径资源。它可以很容易的转变为URI,URL,File对象。尽量使用相对classpath的相对路径。不要使用绝对路径。使用上面 ClassLoaderUtil类的public static URL getExtendResource(String relative...
获取指定目录下所有文件的绝对路径和文件名