public abstract class ResourceUtils
extends java.lang.Object
Consider using Spring's Resource abstraction in the core package
for handling all kinds of file resources in a uniform manner.
ResourceLoader
's getResource()
method can resolve any location to a Resource
object, which in turn allows one to obtain a java.io.File
in the
file system through its getFile()
method.
Resource
,
ClassPathResource
,
FileSystemResource
,
UrlResource
,
ResourceLoader
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CLASSPATH_URL_PREFIX
Pseudo URL prefix for loading from the class path: "classpath:"
|
static java.lang.String |
FILE_URL_PREFIX
URL prefix for loading from the file system: "file:"
|
static java.lang.String |
JAR_FILE_EXTENSION
File extension for a regular jar file: ".jar"
|
static java.lang.String |
JAR_URL_PREFIX
URL prefix for loading from a jar file: "jar:"
|
static java.lang.String |
JAR_URL_SEPARATOR
Separator between JAR URL and file path within the JAR: "!/"
|
static java.lang.String |
URL_PROTOCOL_FILE
URL protocol for a file in the file system: "file"
|
static java.lang.String |
URL_PROTOCOL_JAR
URL protocol for an entry from a jar file: "jar"
|
static java.lang.String |
URL_PROTOCOL_VFS
URL protocol for a general JBoss VFS resource: "vfs"
|
static java.lang.String |
URL_PROTOCOL_VFSFILE
URL protocol for a JBoss file system resource: "vfsfile"
|
static java.lang.String |
URL_PROTOCOL_VFSZIP
URL protocol for an entry from a JBoss jar file: "vfszip"
|
static java.lang.String |
URL_PROTOCOL_WAR
URL protocol for an entry from a war file: "war"
|
static java.lang.String |
URL_PROTOCOL_WSJAR
URL protocol for an entry from a WebSphere jar file: "wsjar"
|
static java.lang.String |
URL_PROTOCOL_ZIP
URL protocol for an entry from a zip file: "zip"
|
static java.lang.String |
WAR_URL_PREFIX
URL prefix for loading from a war file on Tomcat: "war:"
|
static java.lang.String |
WAR_URL_SEPARATOR
Special separator between WAR URL and jar part on Tomcat
|
Constructor and Description |
---|
ResourceUtils() |
Modifier and Type | Method and Description |
---|---|
static java.net.URL |
extractArchiveURL(java.net.URL jarUrl)
Extract the URL for the outermost archive from the given jar/war URL
(which may point to a resource in a jar file or to a jar file itself).
|
static java.net.URL |
extractJarFileURL(java.net.URL jarUrl)
Extract the URL for the actual jar file from the given URL
(which may point to a resource in a jar file or to a jar file itself).
|
static java.io.File |
getFile(java.lang.String resourceLocation)
Resolve the given resource location to a
java.io.File ,
i.e. |
static java.io.File |
getFile(java.net.URI resourceUri)
Resolve the given resource URI to a
java.io.File ,
i.e. |
static java.io.File |
getFile(java.net.URI resourceUri,
java.lang.String description)
Resolve the given resource URI to a
java.io.File ,
i.e. |
static java.io.File |
getFile(java.net.URL resourceUrl)
Resolve the given resource URL to a
java.io.File ,
i.e. |
static java.io.File |
getFile(java.net.URL resourceUrl,
java.lang.String description)
Resolve the given resource URL to a
java.io.File ,
i.e. |
static java.net.URL |
getURL(java.lang.String resourceLocation)
Resolve the given resource location to a
java.net.URL . |
static boolean |
isFileURL(java.net.URL url)
Determine whether the given URL points to a resource in the file system,
i.e.
|
static boolean |
isJarFileURL(java.net.URL url)
Determine whether the given URL points to a jar file itself,
that is, has protocol "file" and ends with the ".jar" extension.
|
static boolean |
isJarURL(java.net.URL url)
Determine whether the given URL points to a resource in a jar file.
|
static boolean |
isUrl(java.lang.String resourceLocation)
Return whether the given resource location is a URL:
either a special "classpath" pseudo URL or a standard URL.
|
static java.net.URI |
toURI(java.lang.String location)
Create a URI instance for the given location String,
replacing spaces with "%20" URI encoding first.
|
static java.net.URI |
toURI(java.net.URL url)
Create a URI instance for the given URL,
replacing spaces with "%20" URI encoding first.
|
static void |
useCachesIfNecessary(java.net.URLConnection con)
Set the
"useCaches" flag on the
given connection, preferring false but leaving the
flag at true for JNLP based resources. |
public static final java.lang.String CLASSPATH_URL_PREFIX
public static final java.lang.String FILE_URL_PREFIX
public static final java.lang.String JAR_URL_PREFIX
public static final java.lang.String WAR_URL_PREFIX
public static final java.lang.String URL_PROTOCOL_FILE
public static final java.lang.String URL_PROTOCOL_JAR
public static final java.lang.String URL_PROTOCOL_WAR
public static final java.lang.String URL_PROTOCOL_ZIP
public static final java.lang.String URL_PROTOCOL_WSJAR
public static final java.lang.String URL_PROTOCOL_VFSZIP
public static final java.lang.String URL_PROTOCOL_VFSFILE
public static final java.lang.String URL_PROTOCOL_VFS
public static final java.lang.String JAR_FILE_EXTENSION
public static final java.lang.String JAR_URL_SEPARATOR
public static final java.lang.String WAR_URL_SEPARATOR
public static boolean isUrl(java.lang.String resourceLocation)
resourceLocation
- the location String to checkCLASSPATH_URL_PREFIX
,
URL
public static java.net.URL getURL(java.lang.String resourceLocation) throws java.io.FileNotFoundException
java.net.URL
.
Does not check whether the URL actually exists; simply returns the URL that the given location would correspond to.
resourceLocation
- the resource location to resolve: either a
"classpath:" pseudo URL, a "file:" URL, or a plain file pathjava.io.FileNotFoundException
- if the resource cannot be resolved to a URLpublic static java.io.File getFile(java.lang.String resourceLocation) throws java.io.FileNotFoundException
java.io.File
,
i.e. to a file in the file system.
Does not check whether the file actually exists; simply returns the File that the given location would correspond to.
resourceLocation
- the resource location to resolve: either a
"classpath:" pseudo URL, a "file:" URL, or a plain file pathjava.io.FileNotFoundException
- if the resource cannot be resolved to
a file in the file systempublic static java.io.File getFile(java.net.URL resourceUrl) throws java.io.FileNotFoundException
java.io.File
,
i.e. to a file in the file system.resourceUrl
- the resource URL to resolvejava.io.FileNotFoundException
- if the URL cannot be resolved to
a file in the file systempublic static java.io.File getFile(java.net.URL resourceUrl, java.lang.String description) throws java.io.FileNotFoundException
java.io.File
,
i.e. to a file in the file system.resourceUrl
- the resource URL to resolvedescription
- a description of the original resource that
the URL was created for (for example, a class path location)java.io.FileNotFoundException
- if the URL cannot be resolved to
a file in the file systempublic static java.io.File getFile(java.net.URI resourceUri) throws java.io.FileNotFoundException
java.io.File
,
i.e. to a file in the file system.resourceUri
- the resource URI to resolvejava.io.FileNotFoundException
- if the URL cannot be resolved to
a file in the file systempublic static java.io.File getFile(java.net.URI resourceUri, java.lang.String description) throws java.io.FileNotFoundException
java.io.File
,
i.e. to a file in the file system.resourceUri
- the resource URI to resolvedescription
- a description of the original resource that
the URI was created for (for example, a class path location)java.io.FileNotFoundException
- if the URL cannot be resolved to
a file in the file systempublic static boolean isFileURL(java.net.URL url)
url
- the URL to checkpublic static boolean isJarURL(java.net.URL url)
url
- the URL to checkpublic static boolean isJarFileURL(java.net.URL url)
url
- the URL to checkpublic static java.net.URL extractJarFileURL(java.net.URL jarUrl) throws java.net.MalformedURLException
jarUrl
- the original URLjava.net.MalformedURLException
- if no valid jar file URL could be extractedpublic static java.net.URL extractArchiveURL(java.net.URL jarUrl) throws java.net.MalformedURLException
In the case of a jar file nested within a war file, this will return a URL to the war file since that is the one resolvable in the file system.
jarUrl
- the original URLjava.net.MalformedURLException
- if no valid jar file URL could be extractedextractJarFileURL(URL)
public static java.net.URI toURI(java.net.URL url) throws java.net.URISyntaxException
url
- the URL to convert into a URI instancejava.net.URISyntaxException
- if the URL wasn't a valid URIURL.toURI()
public static java.net.URI toURI(java.lang.String location) throws java.net.URISyntaxException
location
- the location String to convert into a URI instancejava.net.URISyntaxException
- if the location wasn't a valid URIpublic static void useCachesIfNecessary(java.net.URLConnection con)
"useCaches"
flag on the
given connection, preferring false
but leaving the
flag at true
for JNLP based resources.con
- the URLConnection to set the flag on