一、前言
关于spring-core源码包中org.springframework.util.ResourceUtils资源工具类,进行资源类型判断isUrl、指定类加载classpath转换文件url地址getURL、从类加载路径获取文件getFile、判断是否文件路径isFileURL等操作源码示例。
二、源码说明
package org.springframework.util;@b@@b@import java.io.File;@b@import java.io.FileNotFoundException;@b@import java.net.MalformedURLException;@b@import java.net.URI;@b@import java.net.URISyntaxException;@b@import java.net.URL;@b@import java.net.URLConnection;@b@@b@public abstract class ResourceUtils@b@{@b@ public static final String CLASSPATH_URL_PREFIX = "classpath:";@b@ public static final String FILE_URL_PREFIX = "file:";@b@ public static final String JAR_URL_PREFIX = "jar:";@b@ public static final String WAR_URL_PREFIX = "war:";@b@ public static final String URL_PROTOCOL_FILE = "file";@b@ public static final String URL_PROTOCOL_JAR = "jar";@b@ public static final String URL_PROTOCOL_WAR = "war";@b@ public static final String URL_PROTOCOL_ZIP = "zip";@b@ public static final String URL_PROTOCOL_WSJAR = "wsjar";@b@ public static final String URL_PROTOCOL_VFSZIP = "vfszip";@b@ public static final String URL_PROTOCOL_VFSFILE = "vfsfile";@b@ public static final String URL_PROTOCOL_VFS = "vfs";@b@ public static final String JAR_FILE_EXTENSION = ".jar";@b@ public static final String JAR_URL_SEPARATOR = "!/";@b@ public static final String WAR_URL_SEPARATOR = "*/";@b@@b@ public static boolean isUrl(String resourceLocation)@b@ {@b@ if (resourceLocation == null)@b@ return false;@b@@b@ if (resourceLocation.startsWith("classpath:"))@b@ return true;@b@ try@b@ {@b@ new URL(resourceLocation);@b@ return true;@b@ } catch (MalformedURLException ex) {@b@ }@b@ return false;@b@ }@b@@b@ public static URL getURL(String resourceLocation)@b@ throws FileNotFoundException@b@ {@b@ Assert.notNull(resourceLocation, "Resource location must not be null");@b@ if (resourceLocation.startsWith("classpath:")) {@b@ String path = resourceLocation.substring("classpath:".length());@b@ ClassLoader cl = ClassUtils.getDefaultClassLoader();@b@ URL url = (cl != null) ? cl.getResource(path) : ClassLoader.getSystemResource(path);@b@ if (url == null) {@b@ String description = "class path resource [" + path + "]";@b@ throw new FileNotFoundException(description + " cannot be resolved to URL because it does not exist");@b@ }@b@@b@ return url;@b@ }@b@ try@b@ {@b@ return new URL(resourceLocation);@b@ }@b@ catch (MalformedURLException ex)@b@ {@b@ try {@b@ return new File(resourceLocation).toURI().toURL();@b@ }@b@ catch (MalformedURLException ex2) {@b@ throw new FileNotFoundException("Resource location [" + resourceLocation + "] is neither a URL not a well-formed file path");@b@ }@b@ }@b@ }@b@@b@ public static File getFile(String resourceLocation)@b@ throws FileNotFoundException@b@ {@b@ Assert.notNull(resourceLocation, "Resource location must not be null");@b@ if (resourceLocation.startsWith("classpath:")) {@b@ String path = resourceLocation.substring("classpath:".length());@b@ String description = "class path resource [" + path + "]";@b@ ClassLoader cl = ClassUtils.getDefaultClassLoader();@b@ URL url = (cl != null) ? cl.getResource(path) : ClassLoader.getSystemResource(path);@b@ if (url == null) {@b@ throw new FileNotFoundException(description + " cannot be resolved to absolute file path because it does not exist");@b@ }@b@@b@ return getFile(url, description);@b@ }@b@ try@b@ {@b@ return getFile(new URL(resourceLocation));@b@ }@b@ catch (MalformedURLException ex) {@b@ }@b@ return new File(resourceLocation);@b@ }@b@@b@ public static File getFile(URL resourceUrl)@b@ throws FileNotFoundException@b@ {@b@ return getFile(resourceUrl, "URL");@b@ }@b@@b@ public static File getFile(URL resourceUrl, String description)@b@ throws FileNotFoundException@b@ {@b@ Assert.notNull(resourceUrl, "Resource URL must not be null");@b@ if (!("file".equals(resourceUrl.getProtocol()))) {@b@ throw new FileNotFoundException(description + " cannot be resolved to absolute file path because it does not reside in the file system: " + resourceUrl);@b@ }@b@@b@ try@b@ {@b@ return new File(toURI(resourceUrl).getSchemeSpecificPart());@b@ }@b@ catch (URISyntaxException ex) {@b@ }@b@ return new File(resourceUrl.getFile());@b@ }@b@@b@ public static File getFile(URI resourceUri)@b@ throws FileNotFoundException@b@ {@b@ return getFile(resourceUri, "URI");@b@ }@b@@b@ public static File getFile(URI resourceUri, String description)@b@ throws FileNotFoundException@b@ {@b@ Assert.notNull(resourceUri, "Resource URI must not be null");@b@ if (!("file".equals(resourceUri.getScheme()))) {@b@ throw new FileNotFoundException(description + " cannot be resolved to absolute file path because it does not reside in the file system: " + resourceUri);@b@ }@b@@b@ return new File(resourceUri.getSchemeSpecificPart());@b@ }@b@@b@ public static boolean isFileURL(URL url)@b@ {@b@ String protocol = url.getProtocol();@b@ return (("file".equals(protocol)) || ("vfsfile".equals(protocol)) || @b@ ("vfs"@b@ .equals(protocol)));@b@ }@b@@b@ public static boolean isJarURL(URL url)@b@ {@b@ String protocol = url.getProtocol();@b@ return (("jar".equals(protocol)) || ("war".equals(protocol)) || @b@ ("zip"@b@ .equals(protocol)) || @b@ ("vfszip".equals(protocol)) || @b@ ("wsjar"@b@ .equals(protocol)));@b@ }@b@@b@ public static boolean isJarFileURL(URL url)@b@ {@b@ return (("file".equals(url.getProtocol())) && @b@ (url@b@ .getPath().toLowerCase().endsWith(".jar")));@b@ }@b@@b@ public static URL extractJarFileURL(URL jarUrl)@b@ throws MalformedURLException@b@ {@b@ String urlFile = jarUrl.getFile();@b@ int separatorIndex = urlFile.indexOf("!/");@b@ if (separatorIndex != -1) {@b@ String jarFile = urlFile.substring(0, separatorIndex);@b@ try {@b@ return new URL(jarFile);@b@ }@b@ catch (MalformedURLException ex)@b@ {@b@ if (!(jarFile.startsWith("/")))@b@ jarFile = "/" + jarFile;@b@@b@ return new URL("file:" + jarFile);@b@ }@b@ }@b@@b@ return jarUrl;@b@ }@b@@b@ public static URL extractArchiveURL(URL jarUrl)@b@ throws MalformedURLException@b@ {@b@ String urlFile = jarUrl.getFile();@b@@b@ int endIndex = urlFile.indexOf("*/");@b@ if (endIndex != -1)@b@ {@b@ String warFile = urlFile.substring(0, endIndex);@b@ if ("war".equals(jarUrl.getProtocol()))@b@ return new URL(warFile);@b@@b@ int startIndex = warFile.indexOf("war:");@b@ if (startIndex != -1) {@b@ return new URL(warFile.substring(startIndex + "war:".length()));@b@ }@b@@b@ }@b@@b@ return extractJarFileURL(jarUrl);@b@ }@b@@b@ public static URI toURI(URL url)@b@ throws URISyntaxException@b@ {@b@ return toURI(url.toString());@b@ }@b@@b@ public static URI toURI(String location)@b@ throws URISyntaxException@b@ {@b@ return new URI(StringUtils.replace(location, " ", "%20"));@b@ }@b@@b@ public static void useCachesIfNecessary(URLConnection con)@b@ {@b@ con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP"));@b@ }@b@}