public abstract class

ResourceUtils

extends Object
java.lang.Object
   ↳ org.springframework.util.ResourceUtils

Class Overview

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. ResourceLoader's getResource method can resolve any location to a Resource object, which in turn allows to obtain a java.io.File in the file system through its getFile() method.

The main reason for these utility methods for resource location handling is to support Log4jConfigurer, which must be able to resolve resource locations before the logging system has been initialized. Spring' Resource abstraction in the core package, on the other hand, already expects the logging system to be available.

Summary

Constants
String CLASSPATH_URL_PREFIX Pseudo URL prefix for loading from the class path: "classpath:"
String FILE_URL_PREFIX URL prefix for loading from the file system: "file:"
String JAR_URL_SEPARATOR Separator between JAR URL and file path within the JAR
String URL_PROTOCOL_CODE_SOURCE URL protocol for an entry from an OC4J jar file: "code-source"
String URL_PROTOCOL_FILE URL protocol for a file in the file system: "file"
String URL_PROTOCOL_JAR URL protocol for an entry from a jar file: "jar"
String URL_PROTOCOL_VFS URL protocol for a JBoss VFS resource: "vfs"
String URL_PROTOCOL_VFSZIP URL protocol for an entry from a JBoss jar file: "vfszip"
String URL_PROTOCOL_WSJAR URL protocol for an entry from a WebSphere jar file: "wsjar"
String URL_PROTOCOL_ZIP URL protocol for an entry from a zip file: "zip"
Public Constructors
ResourceUtils()
Public Methods
static URL extractJarFileURL(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 File getFile(URI resourceUri)
Resolve the given resource URI to a java.io.File, i.e.
static File getFile(URL resourceUrl)
Resolve the given resource URL to a java.io.File, i.e.
static File getFile(URI resourceUri, String description)
Resolve the given resource URI to a java.io.File, i.e.
static File getFile(URL resourceUrl, String description)
Resolve the given resource URL to a java.io.File, i.e.
static File getFile(String resourceLocation)
Resolve the given resource location to a java.io.File, i.e.
static URL getURL(String resourceLocation)
Resolve the given resource location to a java.net.URL.
static boolean isFileURL(URL url)
Determine whether the given URL points to a resource in the file system, that is, has protocol "file" or "vfs".
static boolean isJarURL(URL url)
Determine whether the given URL points to a resource in a jar file, that is, has protocol "jar", "zip", "wsjar" or "code-source".
static boolean isUrl(String resourceLocation)
Return whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.
static URI toURI(String location)
Create a URI instance for the given location String, replacing spaces with "%20" quotes first.
static URI toURI(URL url)
Create a URI instance for the given URL, replacing spaces with "%20" quotes first.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String CLASSPATH_URL_PREFIX

Also: SpringCore

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

Constant Value: "classpath:"

public static final String FILE_URL_PREFIX

Also: SpringCore

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

Constant Value: "file:"

public static final String JAR_URL_SEPARATOR

Also: SpringCore

Separator between JAR URL and file path within the JAR

Constant Value: "!/"

public static final String URL_PROTOCOL_CODE_SOURCE

Also: SpringCore

URL protocol for an entry from an OC4J jar file: "code-source"

Constant Value: "code-source"

public static final String URL_PROTOCOL_FILE

Also: SpringCore

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

Constant Value: "file"

public static final String URL_PROTOCOL_JAR

Also: SpringCore

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

Constant Value: "jar"

public static final String URL_PROTOCOL_VFS

Also: SpringCore

URL protocol for a JBoss VFS resource: "vfs"

Constant Value: "vfs"

public static final String URL_PROTOCOL_VFSZIP

Also: SpringCore

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

Constant Value: "vfszip"

public static final String URL_PROTOCOL_WSJAR

Also: SpringCore

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

Constant Value: "wsjar"

public static final String URL_PROTOCOL_ZIP

Also: SpringCore

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

Constant Value: "zip"

Public Constructors

public ResourceUtils ()

Also: SpringCore

Public Methods

public static URL extractJarFileURL (URL jarUrl)

Also: SpringCore

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 the original URL
Returns
  • the URL for the actual jar file
Throws
MalformedURLException if no valid jar file URL could be extracted

public static File getFile (URI resourceUri)

Also: SpringCore

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

Parameters
resourceUri the resource URI to resolve
Returns
  • a corresponding File object
Throws
FileNotFoundException if the URL cannot be resolved to a file in the file system

public static File getFile (URL resourceUrl)

Also: SpringCore

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

Parameters
resourceUrl the resource URL to resolve
Returns
  • a corresponding File object
Throws
FileNotFoundException if the URL cannot be resolved to a file in the file system

public static File getFile (URI resourceUri, String description)

Also: SpringCore

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

Parameters
resourceUri the resource URI to resolve
description a description of the original resource that the URI was created for (for example, a class path location)
Returns
  • a corresponding File object
Throws
FileNotFoundException if the URL cannot be resolved to a file in the file system

public static File getFile (URL resourceUrl, String description)

Also: SpringCore

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

Parameters
resourceUrl the resource URL to resolve
description a description of the original resource that the URL was created for (for example, a class path location)
Returns
  • a corresponding File object
Throws
FileNotFoundException if the URL cannot be resolved to a file in the file system

public static File getFile (String resourceLocation)

Also: SpringCore

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

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

Parameters
resourceLocation the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path
Returns
  • a corresponding File object
Throws
FileNotFoundException if the resource cannot be resolved to a file in the file system

public static URL getURL (String resourceLocation)

Also: SpringCore

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 the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path
Returns
  • a corresponding URL object
Throws
FileNotFoundException if the resource cannot be resolved to a URL

public static boolean isFileURL (URL url)

Also: SpringCore

Determine whether the given URL points to a resource in the file system, that is, has protocol "file" or "vfs".

Parameters
url the URL to check
Returns
  • whether the URL has been identified as a file system URL

public static boolean isJarURL (URL url)

Also: SpringCore

Determine whether the given URL points to a resource in a jar file, that is, has protocol "jar", "zip", "wsjar" or "code-source".

"zip" and "wsjar" are used by BEA WebLogic Server and IBM WebSphere, respectively, but can be treated like jar files. The same applies to "code-source" URLs on Oracle OC4J, provided that the path contains a jar separator.

Parameters
url the URL to check
Returns
  • whether the URL has been identified as a JAR URL

public static boolean isUrl (String resourceLocation)

Also: SpringCore

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

Parameters
resourceLocation the location String to check
Returns
  • whether the location qualifies as a URL

public static URI toURI (String location)

Also: SpringCore

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

Parameters
location the location String to convert into a URI instance
Returns
  • the URI instance
Throws
URISyntaxException if the location wasn't a valid URI

public static URI toURI (URL url)

Also: SpringCore

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

Furthermore, this method works on JDK 1.4 as well, in contrast to the URL.toURI() method.

Parameters
url the URL to convert into a URI instance
Returns
  • the URI instance
Throws
URISyntaxException if the URL wasn't a valid URI
See Also