1.可以在servlet的init方法里
String path = getServletContext().getRealPath("/");
例如 :E:\eclipseM9\workspace\tree\
tree是我web项目的根目录
2.你也可以随时在任意的class里调用
这将获取 到classes目录的全路径
例如 : E:\eclipseM9/workspace/tree/WEB-INF/classes/
3.request.getContextPath();
如 /tree
tree是我的web项目的root context
获取web项目的全路径
Java路径问题最终解决方案 —可定位所有资源的相对路径寻址
前言
Java路径
一、URI形式的绝对资源路径
二、本地系统的绝对路径
三、相对于classpath的相对路径
其中,bin是本项目的classpath。所有的Java源文件编译后的.class文件复制到这个目录中。
四、相对于当前用户目录的相对路径
得到的是当前类FileTest.class文件的URI目录。不包括自己!
如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/com/test/
2,FileTest.class.getResource("/")
得到的是当前的classpath的绝对URI路径。
如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
3,Thread.currentThread().getContextClassLoader().getResource("")
得到的也是当前ClassPath的绝对URI路径。
4,FileTest.class.getClassLoader().getResource("")
5,ClassLoader.getSystemResource("")
得到的也是当前ClassPath的绝对URI路径。
Web应用程序中资源的寻址
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL; import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ClassLoaderUtil { private static Log log=LogFactory.getLog(ClassLoaderUtil.class);
public static Class loadClass(String className{
try {
return getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
thrownew RuntimeException("class not found '"+className+"'", e);
}
}
public static ClassLoader getClassLoader() {
return ClassLoaderUtil.class.getClassLoader();
}
public static InputStream getStream(String relativePath) throws MalformedURLException, IOException {
if(!relativePath.contains("../")){
return getClassLoader().getResourceAsStream(relativePath);
}else{
return ClassLoaderUtil.getStreamByExtendResource(relativePath); } }
public static InputStream getStream(URL url) throws IOException{
if(url!=null){
return url.openStream();
}else{
returnnull;
}
}
public static InputStream getStreamByExtendResource(String relativePath) throws MalformedURLException, IOException{
return ClassLoaderUtil.getStream(ClassLoaderUtil.getExtendResource(relativePath)); }
public static Properties getProperties(String resource) {
Properties properties = new Properties();
try {
properties.load(getStream(resource));
} catch (IOException e) {
thrownew RuntimeException("couldn't load properties file '"+resource+"'", e); }
return properties; }
public static String getAbsolutePathOfClassLoaderClassPath(){
ClassLoaderUtil.log.info(ClassLoaderUtil.getClassLoader().getResource("").toString()); return ClassLoaderUtil.getClassLoader().getResource("").toString();
}
public static URL getExtendResource(String relativePath) throws MalformedURLException{ ClassLoaderUtil.log.info("传入的相对路径:"+relativePath) ; //ClassLoaderUtil.log.info(Integer.valueOf(relativePath.indexOf("../"))) ;
if(!relativePath.contains("../")){
return ClassLoaderUtil.getResource(relativePath);
}
String classPathAbsolutePath=ClassLoaderUtil.getAbsolutePathOfClassLoaderClassPath(); if(relativePath.substring(0, 1).equals("/")){ relativePath=relativePath.substring(1);
}
ClassLoaderUtil.log.info(Integer.valueOf(relativePath.lastIndexOf("../"))) ; String wildcardString=relativePath.substring(0,relativePath.lastIndexOf("../")+3); relativePath=relativePath.substring(relativePath.lastIndexOf("../")+3);
int containSum=ClassLoaderUtil.containSum(wildcardString, "../"); classPathAbsolutePath= ClassLoaderUtil.cutLastString(classPathAbsolutePath, "/", containSum);
String resourceAbsolutePath=classPathAbsolutePath+relativePath; ClassLoaderUtil.log.info("绝对路径:"+resourceAbsolutePath) ;
URL resourceAbsoluteURL=new URL(resourceAbsolutePath);
return resourceAbsoluteURL;
}
private staticint containSum(String source,String dest){
int containSum=0;
int destLength=dest.length();
while(source.contains(dest)){
containSum=containSum+1;
source=source.substring(destLength);
}
return containSum;
}
private static String cutLastString(String source,String dest,int num){
// String cutSource=null;
for(int i=0;i< span="" />
source=source.substring(0, source.lastIndexOf(dest, source.length()-2)+1); }
return source;
}
public static URL getResource(String resource){
ClassLoaderUtil.log.info("传入的相对于classpath的路径:"+resource) ;
return ClassLoaderUtil.getClassLoader().getResource(resource);
}
public staticvoid main(String[] args) throws MalformedURLException { //ClassLoaderUtil.getExtendResource("../spring/dao.xml"); //ClassLoaderUtil.getExtendResource("../../../src/log4j.properties"); ClassLoaderUtil.getExtendResource("log4j.properties"); System.out.println(ClassLoaderUtil.getClassLoader().getResource("log4j.properties").toString());
}
}
Reference:
http://blog.sina.com.cn/s/blog_65ee1e4c0101bk3e.html