原文出處http://java.chinaitlab.com/base/532062.htmlhtml
getResourceAsStream(String name)等方法,使用相對於當前項目的classpath的相對路徑來查找資源。
getResourceAsStream(String name)等方法,使用相對於當前項目的classpath的相對路徑來查找資源。
1 package utils; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.net.MalformedURLException; 6 import java.net.URL; 7 import java.util.Properties; 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 /** 12 * 用來加載類,classpath下的資源文件,屬性文件等。 13 * getExtendResource(StringrelativePath)方法,可使用../符號來加載classpath外部的資源。 14 */ 15 public class ClassLoaderUtil { 16 private static Log logger = LogFactory.getLog(ClassLoaderUtil.class); 17 18 /** *Thread.currentThread().getContextClassLoader().getResource("") */ 19 /** 20 * 加載Java類。 使用全限定類名 21 * 22 * @paramclassName 23 * @return 24 */ 25 public static Class loadClass(String className) { 26 try { 27 return getClassLoader().loadClass(className); 28 } catch (ClassNotFoundException e) { 29 throw new RuntimeException("class not found '" + className + "'", e); 30 } 31 } 32 33 /** 34 * 獲得類加載器 35 * 36 * @return 37 */ 38 public static ClassLoader getClassLoader() { 39 return ClassLoaderUtil.class.getClassLoader(); 40 } 41 42 /** 43 * 提供相對於classpath的資源路徑,返回文件的輸入流 44 * 45 * @param relativePath 46 * 必須傳遞資源的相對路徑。是相對於classpath的路徑。若是須要查找classpath外部的資源,須要使用../來查找 47 * @return 文件輸入流 48 * @throwsIOException 49 * @throwsMalformedURLException 50 */ 51 public static InputStream getStream(String relativePath) 52 throws MalformedURLException, IOException { 53 if (!relativePath.contains("../")) { 54 return getClassLoader().getResourceAsStream(relativePath); 55 } else { 56 return ClassLoaderUtil.getStreamByExtendResource(relativePath); 57 } 58 } 59 60 /** 61 * @param url 62 * @return 63 * @throwsIOException 64 */ 65 public static InputStream getStream(URL url) throws IOException { 66 if (url != null) { 67 return url.openStream(); 68 } else { 69 return null; 70 } 71 } 72 73 /** 74 * @param relativePath 75 * 必須傳遞資源的相對路徑。是相對於classpath的路徑。若是須要查找classpath外部的資源,須要使用../來查找 76 * @return 77 * @throwsMalformedURLException 78 * @throwsIOException 79 */ 80 public static InputStream getStreamByExtendResource(String relativePath) 81 throws MalformedURLException, IOException { 82 return ClassLoaderUtil.getStream(ClassLoaderUtil 83 .getExtendResource(relativePath)); 84 } 85 86 /** 87 * 提供相對於classpath的資源路徑,返回屬性對象,它是一個散列表 88 * 89 * @param resource 90 * @return 91 */ 92 public static Properties getProperties(String resource) { 93 Properties properties = new Properties(); 94 try { 95 properties.load(getStream(resource)); 96 } catch (IOException e) { 97 throw new RuntimeException("couldn't load properties file '" 98 + resource + "'", e); 99 } 100 return properties; 101 } 102 103 /** 104 * 獲得本Class所在的ClassLoader的Classpat的絕對路徑。 URL形式的 105 * 106 * @return 107 */ 108 public static String getAbsolutePathOfClassLoaderClassPath() { 109 ClassLoaderUtil.logger.info(ClassLoaderUtil.getClassLoader() 110 .getResource("").toString()); 111 return ClassLoaderUtil.getClassLoader().getResource("").toString(); 112 } 113 114 /** 115 * @param relativePath 116 * 必須傳遞資源的相對路徑。是相對於classpath的路徑。若是須要查找classpath外部的資源,須要使用../來查找 117 * @return 資源的絕對URL 118 * @throwsMalformedURLException 119 */ 120 public static URL getExtendResource(String relativePath) 121 throws MalformedURLException { 122 ClassLoaderUtil.logger.info("傳入的相對路徑:" + relativePath); 123 // ClassLoaderUtil.log.info(Integer.valueOf(relativePath.indexOf("../"))) 124 // ; 125 if (!relativePath.contains("../")) { 126 return ClassLoaderUtil.getResource(relativePath); 127 } 128 String classPathAbsolutePath = ClassLoaderUtil 129 .getAbsolutePathOfClassLoaderClassPath(); 130 if (relativePath.substring(0, 1).equals("/")) { 131 relativePath = relativePath.substring(1); 132 } 133 ClassLoaderUtil.logger.info(Integer.valueOf(relativePath 134 .lastIndexOf("../"))); 135 String wildcardString = relativePath.substring(0, 136 relativePath.lastIndexOf("../") + 3); 137 relativePath = relativePath 138 .substring(relativePath.lastIndexOf("../") + 3); 139 int containSum = ClassLoaderUtil.containSum(wildcardString, "../"); 140 classPathAbsolutePath = ClassLoaderUtil.cutLastString( 141 classPathAbsolutePath, "/", containSum); 142 String resourceAbsolutePath = classPathAbsolutePath + relativePath; 143 ClassLoaderUtil.logger.info("絕對路徑:" + resourceAbsolutePath); 144 URL resourceAbsoluteURL = new URL(resourceAbsolutePath); 145 return resourceAbsoluteURL; 146 } 147 148 /** 149 * @paramsource 150 * @paramdest 151 * @return 152 */ 153 private static int containSum(String source, String dest) { 154 int containSum = 0; 155 int destLength = dest.length(); 156 while (source.contains(dest)) { 157 containSum = containSum + 1; 158 source = source.substring(destLength); 159 } 160 return containSum; 161 } 162 163 /** 164 * @paramsource 165 * @paramdest 166 * @paramnum 167 * @return 168 */ 169 private static String cutLastString(String source, String dest, int num) { 170 // String cutSource=null; 171 for (int i = 0; i < num; i++) { 172 source = source.substring(0, 173 source.lastIndexOf(dest, source.length() - 2) + 1); 174 } 175 return source; 176 } 177 178 /** 179 * @paramresource 180 * @return 181 */ 182 public static URL getResource(String resource) { 183 ClassLoaderUtil.logger.info("傳入的相對於classpath的路徑:" + resource); 184 185 return ClassLoaderUtil.getClassLoader().getResource(resource); 186 } 187 188 /** 189 * @paramargs 190 * @throwsMalformedURLException 191 */ 192 public static void main(String[] args) throws MalformedURLException { 193 // ClassLoaderUtil.getExtendResource("../spring/dao.xml"); 194 // ClassLoaderUtil.getExtendResource("../../../src/log4j.properties"); 195 ClassLoaderUtil.getExtendResource("log4j.properties"); 196 System.out.println(ClassLoaderUtil.getClassLoader() 197 .getResource("log4j.properties").toString()); 198 } 199 200 }