Top Description Fields Constructors Methods
org.springframework.util

public abstract Class ResourceUtils

extends Object
Class Inheritance
Imports
java.io.File, .FileNotFoundException, java.net.JarURLConnection, .MalformedURLException, .URI, .URISyntaxException, .URL, .URLConnection, org.springframework.lang.Nullable

Utility methods for resolving resource locations to files in the file system. Mainly for internal use within the framework.

Consider using Spring's Resource abstraction in the core package for handling all kinds of file resources in a uniform manner. org.springframework.core.io.ResourceLoader's getResource() method can resolve any location to an org.springframework.core.io.Resource object, which in turn allows one to obtain a java.io.File in the file system through its getFile() method.

Author
Juergen Hoeller
Since
1.1.5
See Also
org.springframework.core.io.Resource, org.springframework.core.io.ClassPathResource, org.springframework.core.io.FileSystemResource, org.springframework.core.io.UrlResource, org.springframework.core.io.ResourceLoader

Field Summary

Modifier and TypeField and Description
public static final String
CLASSPATH_URL_PREFIX

Pseudo URL prefix for loading from the class path: "classpath:".

public static final String
FILE_URL_PREFIX

URL prefix for loading from the file system: "file:".

public static final String
JAR_FILE_EXTENSION

File extension for a regular jar file: ".jar".

public static final String
JAR_URL_PREFIX

URL prefix for loading from a jar file: "jar:".

public static final String
JAR_URL_SEPARATOR

Separator between JAR URL and file path within the JAR: "!/".

public static final String
URL_PROTOCOL_FILE

URL protocol for a file in the file system: "file".

public static final String
URL_PROTOCOL_JAR

URL protocol for an entry from a jar file: "jar".

public static final String
URL_PROTOCOL_VFS

URL protocol for a general JBoss VFS resource: "vfs".

public static final String
URL_PROTOCOL_VFSFILE

URL protocol for a JBoss file system resource: "vfsfile".

public static final String
URL_PROTOCOL_VFSZIP

URL protocol for an entry from a JBoss jar file: "vfszip".

public static final String
URL_PROTOCOL_WAR

URL protocol for an entry from a war file: "war".

public static final String
URL_PROTOCOL_WSJAR

URL protocol for an entry from a WebSphere jar file: "wsjar".

public static final String
URL_PROTOCOL_ZIP

URL protocol for an entry from a zip file: "zip".

public static final String
WAR_URL_PREFIX

URL prefix for loading from a war file on Tomcat: "war:".

public static final String
WAR_URL_SEPARATOR

Special separator between WAR URL and jar part on Tomcat.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static URL

Returns:

the URL for the actual jar file
extractArchiveURL
(URL
the original 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).

public static URL

Returns:

the URL for the actual jar file
extractJarFileURL
(URL
the original 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).

public static File

Returns:

a corresponding File object
getFile
(String
the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path
resourceLocation
)

Resolve the given resource location to a java.io.File, i.e. to a file in the file system.

public static File

Returns:

a corresponding File object
getFile
(URL
the resource URL to resolve
resourceUrl
)

Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.

public static File

Returns:

a corresponding File object
getFile
(URL
the resource URL to resolve
resourceUrl
,
String
a description of the original resource that the URL was created for (for example, a class path location)
description
)

Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.

public static File

Returns:

a corresponding File object
getFile
(URI
the resource URI to resolve
resourceUri
)

Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.

public static File

Returns:

a corresponding File object
getFile
(URI
the resource URI to resolve
resourceUri
,
String
a description of the original resource that the URI was created for (for example, a class path location)
description
)

Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.

public static URL

Returns:

a corresponding URL object
getURL
(String
the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path
resourceLocation
)

Resolve the given resource location to a java.net.URL.

public static boolean

Returns:

whether the URL has been identified as a file system URL
isFileURL
(URL
the URL to check
url
)

Determine whether the given URL points to a resource in the file system, i.e. has protocol "file", "vfsfile" or "vfs".

public static boolean

Returns:

whether the URL has been identified as a JAR file URL
isJarFileURL
(URL
the URL to check
url
)

Determine whether the given URL points to a jar file itself, that is, has protocol "file" and ends with the ".jar" extension.

public static boolean

Returns:

whether the URL has been identified as a JAR URL
isJarURL
(URL
the URL to check
url
)

Determine whether the given URL points to a resource in a jar file — for example, whether the URL has protocol "jar", "war, "zip", "vfszip", or "wsjar".

public static boolean

Returns:

whether the location qualifies as a URL
isUrl
(String
the location String to check
resourceLocation
)

Return whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.

public static URL

Returns:

the relative URL instance
toRelativeURL
(URL
the root URL to start from
root
,
String
the relative path to apply
relativePath
)

Create a URL instance for the given root URL and relative path, going through URI construction and then URL conversion.

public static URI

Returns:

the URI instance
toURI
(URL
the URL to convert into a URI instance
url
)

Create a URI instance for the given URL, replacing spaces with "%20" URI encoding first.

public static URI

Returns:

the URI instance
toURI
(String
the location String to convert into a URI instance
location
)

Create a URI instance for the given location String, replacing spaces with "%20" URI encoding first.

public static URL

Returns:

the URL instance
toURL
(String
the location String to convert into a URL instance
location
)

Create a URL instance for the given location String, going through URI construction and then URL conversion.

public static void
useCachesIfNecessary(URLConnection
the URLConnection to set the flag on
con
)

Set the "useCaches" flag on the given connection, preferring false but leaving the flag at its JVM default value for jar resources (typically true).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CLASSPATH_URL_PREFIXback to summary
public static final String CLASSPATH_URL_PREFIX

Pseudo URL prefix for loading from the class path: "classpath:".

FILE_URL_PREFIXback to summary
public static final String FILE_URL_PREFIX

URL prefix for loading from the file system: "file:".

JAR_FILE_EXTENSIONback to summary
public static final String JAR_FILE_EXTENSION

File extension for a regular jar file: ".jar".

JAR_URL_PREFIXback to summary
public static final String JAR_URL_PREFIX

URL prefix for loading from a jar file: "jar:".

JAR_URL_SEPARATORback to summary
public static final String JAR_URL_SEPARATOR

Separator between JAR URL and file path within the JAR: "!/".

URL_PROTOCOL_FILEback to summary
public static final String URL_PROTOCOL_FILE

URL protocol for a file in the file system: "file".

URL_PROTOCOL_JARback to summary
public static final String URL_PROTOCOL_JAR

URL protocol for an entry from a jar file: "jar".

URL_PROTOCOL_VFSback to summary
public static final String URL_PROTOCOL_VFS

URL protocol for a general JBoss VFS resource: "vfs".

URL_PROTOCOL_VFSFILEback to summary
public static final String URL_PROTOCOL_VFSFILE

URL protocol for a JBoss file system resource: "vfsfile".

URL_PROTOCOL_VFSZIPback to summary
public static final String URL_PROTOCOL_VFSZIP

URL protocol for an entry from a JBoss jar file: "vfszip".

URL_PROTOCOL_WARback to summary
public static final String URL_PROTOCOL_WAR

URL protocol for an entry from a war file: "war".

URL_PROTOCOL_WSJARback to summary
public static final String URL_PROTOCOL_WSJAR

URL protocol for an entry from a WebSphere jar file: "wsjar".

URL_PROTOCOL_ZIPback to summary
public static final String URL_PROTOCOL_ZIP

URL protocol for an entry from a zip file: "zip".

WAR_URL_PREFIXback to summary
public static final String WAR_URL_PREFIX

URL prefix for loading from a war file on Tomcat: "war:".

WAR_URL_SEPARATORback to summary
public static final String WAR_URL_SEPARATOR

Special separator between WAR URL and jar part on Tomcat.

Constructor Detail

ResourceUtilsback to summary
public ResourceUtils()

Method Detail

extractArchiveURLback to summary
public static URL extractArchiveURL(URL jarUrl) throws MalformedURLException

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).

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.

Parameters
jarUrl:URL

the original URL

Returns:URL

the URL for the actual jar file

Exceptions
MalformedURLException:
if no valid jar file URL could be extracted
Since
4.1.8
See Also
extractJarFileURL(URL)
extractJarFileURLback to summary
public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException

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).

Parameters
jarUrl:URL

the original URL

Returns:URL

the URL for the actual jar file

Exceptions
MalformedURLException:
if no valid jar file URL could be extracted
getFileback to summary
public static File getFile(String resourceLocation) throws FileNotFoundException

Resolve the given resource location to a 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.

Parameters
resourceLocation:String

the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path

Returns:File

a corresponding File object

Exceptions
FileNotFoundException:
if the resource cannot be resolved to a file in the file system
getFileback to summary
public static File getFile(URL resourceUrl) throws FileNotFoundException

Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.

Parameters
resourceUrl:URL

the resource URL to resolve

Returns:File

a corresponding File object

Exceptions
FileNotFoundException:
if the URL cannot be resolved to a file in the file system
getFileback to summary
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException

Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.

Parameters
resourceUrl:URL

the resource URL to resolve

description:String

a description of the original resource that the URL was created for (for example, a class path location)

Returns:File

a corresponding File object

Exceptions
FileNotFoundException:
if the URL cannot be resolved to a file in the file system
getFileback to summary
public static File getFile(URI resourceUri) throws FileNotFoundException

Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.

Parameters
resourceUri:URI

the resource URI to resolve

Returns:File

a corresponding File object

Exceptions
FileNotFoundException:
if the URL cannot be resolved to a file in the file system
Since
2.5
getFileback to summary
public static File getFile(URI resourceUri, String description) throws FileNotFoundException

Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.

Parameters
resourceUri:URI

the resource URI to resolve

description:String

a description of the original resource that the URI was created for (for example, a class path location)

Returns:File

a corresponding File object

Exceptions
FileNotFoundException:
if the URL cannot be resolved to a file in the file system
Since
2.5
getURLback to summary
public static URL getURL(String resourceLocation) throws FileNotFoundException

Resolve the given resource location to a java.net.URL.

Does not check whether the URL actually exists; simply returns the URL that the given location would correspond to.

Parameters
resourceLocation:String

the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path

Returns:URL

a corresponding URL object

Exceptions
FileNotFoundException:
if the resource cannot be resolved to a URL
isFileURLback to summary
public static boolean isFileURL(URL url)

Determine whether the given URL points to a resource in the file system, i.e. has protocol "file", "vfsfile" or "vfs".

Parameters
url:URL

the URL to check

Returns:boolean

whether the URL has been identified as a file system URL

isJarFileURLback to summary
public static boolean isJarFileURL(URL url)

Determine whether the given URL points to a jar file itself, that is, has protocol "file" and ends with the ".jar" extension.

Parameters
url:URL

the URL to check

Returns:boolean

whether the URL has been identified as a JAR file URL

Since
4.1
isJarURLback to summary
public static boolean isJarURL(URL url)

Determine whether the given URL points to a resource in a jar file — for example, whether the URL has protocol "jar", "war, "zip", "vfszip", or "wsjar".

Parameters
url:URL

the URL to check

Returns:boolean

whether the URL has been identified as a JAR URL

isUrlback to summary
public static boolean isUrl(String resourceLocation)

Return whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.

Parameters
resourceLocation:String

the location String to check

Returns:boolean

whether the location qualifies as a URL

See Also
CLASSPATH_URL_PREFIX, java.net.URL
toRelativeURLback to summary
public static URL toRelativeURL(URL root, String relativePath) throws MalformedURLException

Create a URL instance for the given root URL and relative path, going through URI construction and then URL conversion.

Parameters
root:URL

the root URL to start from

relativePath:String

the relative path to apply

Returns:URL

the relative URL instance

Exceptions
MalformedURLException:
if the end result is not a valid URL
Since
6.0
toURIback to summary
public static URI toURI(URL url) throws URISyntaxException

Create a URI instance for the given URL, replacing spaces with "%20" URI encoding first.

Parameters
url:URL

the URL to convert into a URI instance

Returns:URI

the URI instance

Exceptions
URISyntaxException:
if the URL wasn't a valid URI
See Also
java.net.URL#toURI()
toURIback to summary
public static URI toURI(String location) throws URISyntaxException

Create a URI instance for the given location String, replacing spaces with "%20" URI encoding first.

Parameters
location:String

the location String to convert into a URI instance

Returns:URI

the URI instance

Exceptions
URISyntaxException:
if the location wasn't a valid URI
toURLback to summary
public static URL toURL(String location) throws MalformedURLException

Create a URL instance for the given location String, going through URI construction and then URL conversion.

Parameters
location:String

the location String to convert into a URL instance

Returns:URL

the URL instance

Annotations
@SuppressWarnings:deprecation
Exceptions
MalformedURLException:
if the location wasn't a valid URL
Since
6.0
useCachesIfNecessaryback to summary
public static void useCachesIfNecessary(URLConnection con)

Set the "useCaches" flag on the given connection, preferring false but leaving the flag at its JVM default value for jar resources (typically true).

Parameters
con:URLConnection

the URLConnection to set the flag on