📄 classloaderutil.java
字号:
/**
*
*/
package net.sf.oxmled.common.util;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author 沈东良 shendl_s@hotmail.com Nov 29, 2006 10:34:34 AM
* 用来加载类,classpath下的资源文件,属性文件等。 getExtendResource(String
* relativePath)方法,可以使用../符号来加载classpath外部的资源。
*/
public class ClassLoaderUtil {
/**
*
*/
private ClassLoaderUtil() {
}
private static Log log = LogFactory.getLog(ClassLoaderUtil.class);
/**
* Thread.currentThread().getContextClassLoader().getResource("")
*/
/**
* 加载Java类。 使用全限定类名
*
* @param className
* @return
*/
public static Class loadClass(String className) {
try {
return getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException("class not found '" + className + "'", e);
}
}
/**
* 得到类加载器
*
* @return
*/
public static ClassLoader getClassLoader() {
return ClassLoaderUtil.class.getClassLoader();
}
/**
* 提供相对于classpath的资源路径,返回文件的输入流
*
* @param relativePath
* 必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找
* @return 文件输入流
* @throws IOException
* @throws MalformedURLException
*/
public static InputStream getStream(String relativePath)
throws MalformedURLException, IOException {
/*
* if(!relativePath.contains("../")){ return
* getClassLoader().getResourceAsStream(relativePath);
*
* }else{ return
* ClassLoaderUtil.getStreamByExtendResource(relativePath); }
*/
if (relativePath.indexOf("../") != -1) {
return getClassLoader().getResourceAsStream(relativePath);
} else {
return ClassLoaderUtil.getStreamByExtendResource(relativePath);
}
}
/**
*
* @param url
* @return
* @throws IOException
*/
public static InputStream getStream(URL url) throws IOException {
if (url != null) {
return url.openStream();
} else {
return null;
}
}
/**
*
* @param
* relativePath必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找
* @return
* @throws MalformedURLException
* @throws IOException
*/
public static InputStream getStreamByExtendResource(String relativePath)
throws MalformedURLException, IOException {
return ClassLoaderUtil.getStream(ClassLoaderUtil
.getExtendResource(relativePath));
}
/**
* 提供相对于classpath的资源路径,返回属性对象,它是一个散列表
*
* @param resource
* @return
*/
public static Properties getProperties(String resource) {
Properties properties = new Properties();
try {
properties.load(getStream(resource));
} catch (IOException e) {
throw new RuntimeException("couldn't load properties file '"
+ resource + "'", e);
}
return properties;
}
/**
* 得到本Class所在的ClassLoader的Classpat的绝对路径。 URL形式的
*
* @return
*/
public static String getAbsolutePathOfClassLoaderClassPath() {
ClassLoaderUtil.log.info(ClassLoaderUtil.getClassLoader().getResource(
"").toString());
return ClassLoaderUtil.getClassLoader().getResource("").toString();
}
/**
*
* @param relativePath
* 必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找
* @return 资源的绝对URL
* @throws MalformedURLException
*/
public static URL getExtendResource(String relativePath)
throws MalformedURLException {
/**
* 对于以/../或者../开头的东西,使用这个方法,否则,仍然调用下面的方法。
* 1,一个/../或者../,就把classpathAbsolutePath的结尾,去掉一个*\/这样的目录。
* 同时,把这个/../或者../也截掉。 2,最后把它们加起来。返回URL new URL(String)
* ===================== 1,去掉开头的/ 2,88/ 3,去掉相应的头的几个结尾的文件夹。
*
*/
ClassLoaderUtil.log.info("传入的相对路径:" + relativePath);
// ClassLoaderUtil.log.info(Integer.valueOf(relativePath.indexOf("../")))
// ;
if (relativePath.indexOf("../") == -1) {
return ClassLoaderUtil.getResource(relativePath);
}
String classPathAbsolutePath = ClassLoaderUtil
.getAbsolutePathOfClassLoaderClassPath();
if (relativePath.substring(0, 1).equals("/")) {
relativePath = relativePath.substring(1);
}
ClassLoaderUtil.log.info(new Integer(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;
}
/**
*
* @param source
* @param dest
* @return
*/
private static int containSum(String source, String dest) {
int containSum = 0;
int destLength = dest.length();
while (source.indexOf(dest) >= 0) {
containSum = containSum + 1;
source = source.substring(destLength);
}
return containSum;
}
/**
*
* @param source
* @param dest
* @param num
* @return
*/
private static String cutLastString(String source, String dest, int num) {
// String cutSource=null;
for (int i = 0; i < num; i++) {
source = source.substring(0, source.lastIndexOf(dest, source
.length() - 2) + 1);
}
return source;
}
/**
* 创建
* @param relativeFileName
* @return
* @throws URISyntaxException
*/
public static URI createURI(String relativeFileName) throws URISyntaxException{
return ClassLoaderUtil.createResource(relativeFileName);
}
/**
* 创建
* @param relativeFileName
* @return
* @throws URISyntaxException
*/
public static URI createResource(String relativeFileName) throws URISyntaxException{
return new URI(ClassLoaderUtil.getResource("").toString()+relativeFileName);
}
/**
*
* @param resource
* @return
*/
public static URL getResource(String resource) {
ClassLoaderUtil.log.info("传入的相对于classpath的路径:" + resource);
return ClassLoaderUtil.getClassLoader().getResource(resource);
}
/**
* @param args
* @throws MalformedURLException
*/
public static void main(String[] args) throws MalformedURLException {
/**
* file:/D:/java/eclipse32/workfolder/wcms/webapps/WEB-INF/classes/
* file:/D:/java/eclipse32/workfolder/wcms/webapps/WEB-INF/classes/
*/
// System.out.println(ClassLoaderUtil.getAbsolutePathOfClassLoaderClassPath());
// ClassLoaderUtil.getExtendResource("../spring/dao.xml");
// ClassLoaderUtil.getExtendResource("../../../src/log4j.properties");
ClassLoaderUtil.getExtendResource("log4j.properties");
System.out.println(ClassLoaderUtil.getClassLoader().getResource(
"log4j.properties").toString());
System.out.println(ClassLoaderUtil.getResource(""));
System.out.println(ClassLoaderUtil.getResource("config/config.xml"));
System.out.println(ClassLoaderUtil.getResource("config/config1.xml"));
System.out.println(ClassLoaderUtil.getResource(""));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -