1 16 17 package org.springframework.util; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.net.URLDecoder ; 24 25 50 public abstract class ResourceUtils { 51 52 53 public static final String CLASSPATH_URL_PREFIX = "classpath:"; 54 55 56 public static final String FILE_URL_PREFIX = "file:"; 57 58 59 public static final String URL_PROTOCOL_FILE = "file"; 60 61 62 public static final String URL_PROTOCOL_JAR = "jar"; 63 64 65 public static final String URL_PROTOCOL_ZIP = "zip"; 66 67 68 public static final String URL_PROTOCOL_WSJAR = "wsjar"; 69 70 71 public static final String JAR_URL_SEPARATOR = "!/"; 72 73 74 80 public static boolean isUrl(String resourceLocation) { 81 if (resourceLocation == null) { 82 return false; 83 } 84 if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) { 85 return true; 86 } 87 try { 88 new URL (resourceLocation); 89 return true; 90 } 91 catch (MalformedURLException ex) { 92 return false; 93 } 94 } 95 96 105 public static URL getURL(String resourceLocation) throws FileNotFoundException { 106 Assert.notNull(resourceLocation, "Resource location must not be null"); 107 if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) { 108 String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length()); 109 URL url = ClassUtils.getDefaultClassLoader().getResource(path); 110 if (url == null) { 111 String description = "class path resource [" + path + "]"; 112 throw new FileNotFoundException ( 113 description + " cannot be resolved to URL because it does not exist"); 114 } 115 return url; 116 } 117 try { 118 return new URL (resourceLocation); 120 } 121 catch (MalformedURLException ex) { 122 try { 124 return new URL (FILE_URL_PREFIX + resourceLocation); 125 } 126 catch (MalformedURLException ex2) { 127 throw new FileNotFoundException ("Resource location [" + resourceLocation + 128 "] is neither a URL not a well-formed file path"); 129 } 130 } 131 } 132 133 144 public static File getFile(String resourceLocation) throws FileNotFoundException { 145 Assert.notNull(resourceLocation, "Resource location must not be null"); 146 if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) { 147 String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length()); 148 String description = "class path resource [" + path + "]"; 149 URL url = ClassUtils.getDefaultClassLoader().getResource(path); 150 if (url == null) { 151 throw new FileNotFoundException ( 152 description + " cannot be resolved to absolute file path " + 153 "because it does not reside in the file system"); 154 } 155 return getFile(url, description); 156 } 157 try { 158 return getFile(new URL (resourceLocation)); 160 } 161 catch (MalformedURLException ex) { 162 return new File (resourceLocation); 164 } 165 } 166 167 175 public static File getFile(URL resourceUrl) throws FileNotFoundException { 176 return getFile(resourceUrl, "URL"); 177 } 178 179 189 public static File getFile(URL resourceUrl, String description) throws FileNotFoundException { 190 Assert.notNull(resourceUrl, "Resource URL must not be null"); 191 if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) { 192 throw new FileNotFoundException ( 193 description + " cannot be resolved to absolute file path " + 194 "because it does not reside in the file system: " + resourceUrl); 195 } 196 return new File (URLDecoder.decode(resourceUrl.getFile())); 197 } 198 199 206 public static boolean isJarURL(URL url) { 207 String protocol = url.getProtocol(); 208 return (URL_PROTOCOL_JAR.equals(protocol) || 209 URL_PROTOCOL_ZIP.equals(protocol) || 210 URL_PROTOCOL_WSJAR.equals(protocol)); 211 } 212 213 220 public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException { 221 String urlFile = jarUrl.getFile(); 222 int separatorIndex = urlFile.indexOf(JAR_URL_SEPARATOR); 223 if (separatorIndex != -1) { 224 String jarFile = urlFile.substring(0, separatorIndex); 225 try { 226 return new URL (jarFile); 227 } 228 catch (MalformedURLException ex) { 229 if (!jarFile.startsWith("/")) { 232 jarFile = "/" + jarFile; 233 } 234 return new URL (FILE_URL_PREFIX + jarFile); 235 } 236 } 237 else { 238 return jarUrl; 239 } 240 } 241 242 } 243 | Popular Tags |