Package org.springframework.util
Class ResourceUtils
java.lang.Object
org.springframework.util.ResourceUtils
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 one to obtain a java.io.File in the
 file system through its getFile() method.
- Since:
- 1.1.5
- Author:
- Juergen Hoeller
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringPseudo URL prefix for loading from the class path: "classpath:".static final StringURL prefix for loading from the file system: "file:".static final StringFile extension for a regular jar file: ".jar".static final StringURL prefix for loading from a jar file: "jar:".static final StringSeparator between JAR URL and file path within the JAR: "!/".static final StringURL protocol for a file in the file system: "file".static final StringURL protocol for an entry from a jar file: "jar".static final StringURL protocol for a general JBoss VFS resource: "vfs".static final StringURL protocol for a JBoss file system resource: "vfsfile".static final StringURL protocol for an entry from a JBoss jar file: "vfszip".static final StringURL protocol for an entry from a war file: "war".static final StringURL protocol for an entry from a WebSphere jar file: "wsjar".static final StringURL protocol for an entry from a zip file: "zip".static final StringURL prefix for loading from a war file on Tomcat: "war:".static final StringSpecial separator between WAR URL and jar part on Tomcat.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic URLextractArchiveURL(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 URLextractJarFileURL(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 FileResolve the given resource location to ajava.io.File, i.e.static FileResolve the given resource URI to ajava.io.File, i.e.static FileResolve the given resource URI to ajava.io.File, i.e.static FileResolve the given resource URL to ajava.io.File, i.e.static FileResolve the given resource URL to ajava.io.File, i.e.static URLResolve the given resource location to ajava.net.URL.static booleanDetermine whether the given URL points to a resource in the file system, i.e.static booleanisJarFileURL(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 booleanDetermine 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".static booleanReturn whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.static URLtoRelativeURL(URL root, String relativePath) Create a clean URL instance for the given root URL and relative path, going through URI construction and then URL conversion.static URICreate a URI instance for the given location String, replacing spaces with "%20" URI encoding first.static URICreate a URI instance for the given URL, replacing spaces with "%20" URI encoding first.static URLCreate a clean URL instance for the given location String, going through URI construction and then URL conversion.static voidSet the"useCaches"flag on the given connection, preferringfalsebut leaving the flag at its JVM default value for jar resources (typicallytrue).
- 
Field Details- 
CLASSPATH_URL_PREFIXPseudo URL prefix for loading from the class path: "classpath:".- See Also:
 
- 
FILE_URL_PREFIXURL prefix for loading from the file system: "file:".- See Also:
 
- 
JAR_URL_PREFIXURL prefix for loading from a jar file: "jar:".- See Also:
 
- 
WAR_URL_PREFIXURL prefix for loading from a war file on Tomcat: "war:".- See Also:
 
- 
URL_PROTOCOL_FILEURL protocol for a file in the file system: "file".- See Also:
 
- 
URL_PROTOCOL_JARURL protocol for an entry from a jar file: "jar".- See Also:
 
- 
URL_PROTOCOL_WARURL protocol for an entry from a war file: "war".- See Also:
 
- 
URL_PROTOCOL_ZIPURL protocol for an entry from a zip file: "zip".- See Also:
 
- 
URL_PROTOCOL_WSJARURL protocol for an entry from a WebSphere jar file: "wsjar".- See Also:
 
- 
URL_PROTOCOL_VFSZIPURL protocol for an entry from a JBoss jar file: "vfszip".- See Also:
 
- 
URL_PROTOCOL_VFSFILEURL protocol for a JBoss file system resource: "vfsfile".- See Also:
 
- 
URL_PROTOCOL_VFSURL protocol for a general JBoss VFS resource: "vfs".- See Also:
 
- 
JAR_FILE_EXTENSIONFile extension for a regular jar file: ".jar".- See Also:
 
- 
JAR_URL_SEPARATORSeparator between JAR URL and file path within the JAR: "!/".- See Also:
 
- 
WAR_URL_SEPARATORSpecial separator between WAR URL and jar part on Tomcat.- See Also:
 
 
- 
- 
Constructor Details- 
ResourceUtilspublic ResourceUtils()
 
- 
- 
Method Details- 
isUrlReturn 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
- See Also:
 
- 
getURLResolve the given resource location to ajava.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
- See Also:
 
- 
getFileResolve the given resource location to ajava.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- 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
- See Also:
 
- 
getFileResolve the given resource URL to ajava.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
- See Also:
 
- 
getFileResolve the given resource URL to ajava.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
 
- 
getFileResolve the given resource URI to ajava.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
- Since:
- 2.5
- See Also:
 
- 
getFileResolve the given resource URI to ajava.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
- Since:
- 2.5
 
- 
isFileURLDetermine whether the given URL points to a resource in the file system, i.e. has protocol "file", "vfsfile" or "vfs".- Parameters:
- url- the URL to check
- Returns:
- whether the URL has been identified as a file system URL
- See Also:
 
- 
isJarURLDetermine 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- the URL to check
- Returns:
- whether the URL has been identified as a JAR URL
- See Also:
 
- 
isJarFileURLDetermine whether the given URL points to a jar file itself, that is, has protocol "file" and ends with the ".jar" extension.- Parameters:
- url- the URL to check
- Returns:
- whether the URL has been identified as a JAR file URL
- Since:
- 4.1
- See Also:
 
- 
extractJarFileURLExtract 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
- See Also:
 
- 
extractArchiveURLExtract 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- the original URL
- Returns:
- the URL for the actual jar file
- Throws:
- MalformedURLException- if no valid jar file URL could be extracted
- Since:
- 4.1.8
- See Also:
 
- 
toURICreate a URI instance for the given URL, replacing spaces with "%20" URI encoding first.- 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:
 
- 
toURICreate a URI instance for the given location String, replacing spaces with "%20" URI encoding 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
- See Also:
 
- 
toURLCreate a clean URL instance for the given location String, going through URI construction and then URL conversion.- Parameters:
- location- the location String to convert into a URL instance
- Returns:
- the URL instance
- Throws:
- MalformedURLException- if the location wasn't a valid URL
- Since:
- 6.0
- See Also:
 
- 
toRelativeURLCreate a clean URL instance for the given root URL and relative path, going through URI construction and then URL conversion.- Parameters:
- root- the root URL to start from
- relativePath- the relative path to apply
- Returns:
- the relative URL instance
- Throws:
- MalformedURLException- if the end result is not a valid URL
- Since:
- 6.0
- See Also:
 
- 
useCachesIfNecessarySet the"useCaches"flag on the given connection, preferringfalsebut leaving the flag at its JVM default value for jar resources (typicallytrue).- Parameters:
- con- the URLConnection to set the flag on
- See Also:
 
 
-