1 packagecom.Utils;2
3 importjava.io.IOException;4 importjava.io.InputStream;5 importjava.net.MalformedURLException;6 importjava.net.URL;7 importjava.util.Properties;8
9 importorg.apache.commons.logging.Log;10 importorg.apache.commons.logging.LogFactory;11
12 /**
13 *@author沈东良shendl_s@hotmail.com14 *Nov29,2006 10:34:34AM15 *用来加载类,classpath下的资源文件,属性文件等。16 *getExtendResource(StringrelativePath)方法,可以使用../符号来加载classpath外部的资源。17 */
18 public classClassLoaderUtil {19 private static Log log=LogFactory.getLog(ClassLoaderUtil.class);20 /**
21 *Thread.currentThread().getContextClassLoader().getResource("")22 */
23
24 /**
25 *加载Java类。 使用全限定类名26 *@paramclassName27 *@return
28 */
29 public staticClass loadClass(String className) {30 try{31 returngetClassLoader().loadClass(className);32 } catch(ClassNotFoundException e) {33 throw new RuntimeException("class not found '"+className+"'", e);34 }35 }36 /**
37 *得到类加载器38 *@return
39 */
40 public staticClassLoader getClassLoader() {41
42 return ClassLoaderUtil.class.getClassLoader();43 }44 /**
45 *提供相对于classpath的资源路径,返回文件的输入流46 *@paramrelativePath必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找47 *@return文件输入流48 *@throwsIOException49 *@throwsMalformedURLException50 */
51 public static InputStream getStream(String relativePath) throwsMalformedURLException, IOException {52 if(!relativePath.contains("../")){53 returngetClassLoader().getResourceAsStream(relativePath);54
55 }else{56 returnClassLoaderUtil.getStreamByExtendResource(relativePath);57 }58
59 }60 /**
61 *62 *@paramurl63 *@return
64 *@throwsIOException65 */
66 public static InputStream getStream(URL url) throwsIOException{67 if(url!=null){68
69 returnurl.openStream();70
71
72 }else{73 return null;74 }75 }76 /**
77 *78 *@paramrelativePath必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找79 *@return
80 *@throwsMalformedURLException81 *@throwsIOException82 */
83 public static InputStream getStreamByExtendResource(String relativePath) throwsMalformedURLException, IOException{84 returnClassLoaderUtil.getStream(ClassLoaderUtil.getExtendResource(relativePath));85
86
87 }88
89 /**
90 *提供相对于classpath的资源路径,返回属性对象,它是一个散列表91 *@paramresource92 *@return
93 */
94 public staticProperties getProperties(String resource) {95 Properties properties = newProperties();96 try{97 properties.load(getStream(resource));98 } catch(IOException e) {99 throw new RuntimeException("couldn't load properties file '"+resource+"'", e);100 }101 returnproperties;102 }103 /**
104 *得到本Class所在的ClassLoader的Classpat的绝对路径。105 *URL形式的106 *@return
107 */
108 public staticString getAbsolutePathOfClassLoaderClassPath(){109
110
111 ClassLoaderUtil.log.info(ClassLoaderUtil.getClassLoader().getResource("").toString());112 return ClassLoaderUtil.getClassLoader().getResource("").toString();113
114 }115 /**
116 *117 *@paramrelativePath 必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找118 *@return资源的绝对URL119 *@throwsMalformedURLException120 */
121 public static URL getExtendResource(String relativePath) throwsMalformedURLException{122
123 ClassLoaderUtil.log.info("传入的相对路径:"+relativePath) ;124 //ClassLoaderUtil.log.info(Integer.valueOf(relativePath.indexOf("../"))) ;
125 if(!relativePath.contains("../")){126 returnClassLoaderUtil.getResource(relativePath);127
128 }129 //得到类加载器的绝对路径
130 String classPathAbsolutePath=ClassLoaderUtil.getAbsolutePathOfClassLoaderClassPath();131 ClassLoaderUtil.log.info("classPathAbsolutePath:"+classPathAbsolutePath);132 //如果是当前目录的路径 ./
133 if(relativePath.substring(0, 1).equals("/")){134 relativePath=relativePath.substring(1);135 System.out.println(relativePath);136 }137 ClassLoaderUtil.log.info(Integer.valueOf(relativePath.lastIndexOf("../"))) ;138
139 String wildcardString=relativePath.substring(0,relativePath.lastIndexOf("../")+3);140 //../../得到其后面的路径
141 relativePath=relativePath.substring(relativePath.lastIndexOf("../")+3);142 int containSum=ClassLoaderUtil.containSum(wildcardString, "../");143 classPathAbsolutePath= ClassLoaderUtil.cutLastString(classPathAbsolutePath, "/", containSum);144 System.out.println(classPathAbsolutePath);145 String resourceAbsolutePath=classPathAbsolutePath+relativePath;146 ClassLoaderUtil.log.info("绝对路径:"+resourceAbsolutePath) ;147 URL resourceAbsoluteURL=newURL(resourceAbsolutePath);148 returnresourceAbsoluteURL;149 }150 /**
151 *看source里面包含多少个dest152 *@paramsource153 *@paramdest154 *@return
155 */
156 private static intcontainSum(String source,String dest){157 int containSum=0;158 int destLength=dest.length();159 while(source.contains(dest)){160 containSum=containSum+1;161 source=source.substring(destLength);162
163 }164 returncontainSum;165 }166 /**
167 *168 *@paramsource169 *@paramdest170 *@paramnum171 *@return
172 */
173 private static String cutLastString(String source,String dest,intnum){174 //String cutSource=null;
175 for(int i=0;i
179 returnsource;180 }181 /**
182 *183 *@paramresource184 *@return
185 */
186 public staticURL getResource(String resource){187 ClassLoaderUtil.log.info("传入的相对于classpath的路径:"+resource) ;188 returnClassLoaderUtil.getClassLoader().getResource(resource);189 }190
191
192
193
194 /**
195 *@paramargs196 *@throwsMalformedURLException197 */
198 public static void main(String[] args) throwsMalformedURLException {199
200 //ClassLoaderUtil.getExtendResource("../spring/dao.xml");201 //ClassLoaderUtil.getExtendResource("../../../src/log4j.properties");202 //ClassLoaderUtil.getExtendResource("log4j.properties");203 //System.out.println(ClassLoaderUtil.getClassLoader().getResource("log4j.properties").toString());
204 String a="abcd/efg/hijk/lmn";205 System.out.println(a.lastIndexOf("/", 3));206 }207
208 }