Class UrlResource
- All Implemented Interfaces:
- InputStreamSource,- Resource
- Direct Known Subclasses:
- FileUrlResource
Resource implementation for java.net.URL locators.
 Supports resolution as a URL and also as a File in
 case of the "file:" protocol.- Since:
- 28.12.2003
- Author:
- Juergen Hoeller, Sam Brannen
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionUrlResource(String path) Create a newUrlResourcebased on a URI path.UrlResource(String protocol, String location) Create a newUrlResourcebased on a URI specification.UrlResource(String protocol, String location, @Nullable String fragment) Create a newUrlResourcebased on a URI specification.UrlResource(URI uri) Create a newUrlResourcebased on the given URI object.UrlResource(URL url) Create a newUrlResourcebased on the given URL object.
- 
Method SummaryModifier and TypeMethodDescriptioncreateRelative(String relativePath) This implementation creates aUrlResource, delegating tocreateRelativeURL(String)for adapting the relative path.protected URLcreateRelativeURL(String relativePath) This delegate creates ajava.net.URL, applying the given path relative to the path of the underlying URL of this resource descriptor.protected voidCustomize the givenURLConnectionbefore fetching the resource.booleanThis implementation compares the underlying URL references.static UrlResourceCreate a newUrlResourcefrom the given URL path.static UrlResourceCreate a newUrlResourcefrom the givenURI.This implementation returns a description that includes the URL.getFile()This implementation returns a File reference for the underlying URL/URI, provided that it refers to a file in the file system.This implementation returns the URL-decoded name of the file that this URL refers to.This implementation opens an InputStream for the given URL.getURI()This implementation returns the underlying URI directly, if possible.getURL()This implementation returns the underlying URL reference.inthashCode()This implementation returns the hash code of the underlying URL reference.booleanisFile()This implementation always returnsfalse.Methods inherited from class org.springframework.core.io.AbstractFileResolvingResourcecontentLength, customizeConnection, exists, getFile, getFileForLastModifiedCheck, isFile, isReadable, lastModified, readableChannelMethods inherited from class org.springframework.core.io.AbstractResourceisOpen, toStringMethods inherited from class java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.core.io.ResourcegetContentAsByteArray, getContentAsString
- 
Constructor Details- 
UrlResourceCreate a newUrlResourcebased on the given URL object.- Parameters:
- url- a URL
- See Also:
 
- 
UrlResourceCreate a newUrlResourcebased on the given URI object.- Parameters:
- uri- a URI
- Throws:
- MalformedURLException- if the given URL path is not valid
- Since:
- 2.5
 
- 
UrlResourceCreate a newUrlResourcebased on a URI path.Note: The given path needs to be pre-encoded if necessary. - Parameters:
- path- a URI path
- Throws:
- MalformedURLException- if the given URI path is not valid
- See Also:
 
- 
UrlResourceCreate a newUrlResourcebased on a URI specification.The given parts will automatically get encoded if necessary. - Parameters:
- protocol- the URL protocol to use (for example, "jar" or "file" - without colon); also known as "scheme"
- location- the location (for example, the file path within that protocol); also known as "scheme-specific part"
- Throws:
- MalformedURLException- if the given URL specification is not valid
- See Also:
 
- 
UrlResourcepublic UrlResource(String protocol, String location, @Nullable String fragment) throws MalformedURLException Create a newUrlResourcebased on a URI specification.The given parts will automatically get encoded if necessary. - Parameters:
- protocol- the URL protocol to use (for example, "jar" or "file" - without colon); also known as "scheme"
- location- the location (for example, the file path within that protocol); also known as "scheme-specific part"
- fragment- the fragment within that location (for example, anchor on an HTML page, as following after a "#" separator)
- Throws:
- MalformedURLException- if the given URL specification is not valid
- See Also:
 
 
- 
- 
Method Details- 
fromCreate a newUrlResourcefrom the givenURI.This factory method is a convenience for UrlResource(URI)that catches anyMalformedURLExceptionand rethrows it wrapped in anUncheckedIOException; suitable for use inStreamandOptionalAPIs or other scenarios when a checkedIOExceptionis undesirable.- Parameters:
- uri- a URI
- Throws:
- UncheckedIOException- if the given URL path is not valid
- Since:
- 6.0
- See Also:
 
- 
fromCreate a newUrlResourcefrom the given URL path.This factory method is a convenience for UrlResource(String)that catches anyMalformedURLExceptionand rethrows it wrapped in anUncheckedIOException; suitable for use inStreamandOptionalAPIs or other scenarios when a checkedIOExceptionis undesirable.- Parameters:
- path- a URL path
- Throws:
- UncheckedIOException- if the given URL path is not valid
- Since:
- 6.0
- See Also:
 
- 
getInputStreamThis implementation opens an InputStream for the given URL.It sets the useCachesflag tofalse, mainly to avoid jar file locking on Windows.- Returns:
- the input stream for the underlying resource (must not be null)
- Throws:
- IOException- if the content stream could not be opened
- See Also:
 
- 
customizeConnectionDescription copied from class:AbstractFileResolvingResourceCustomize the givenURLConnectionbefore fetching the resource.Calls ResourceUtils.useCachesIfNecessary(URLConnection)and delegates toAbstractFileResolvingResource.customizeConnection(HttpURLConnection)if possible. Can be overridden in subclasses.- Overrides:
- customizeConnectionin class- AbstractFileResolvingResource
- Parameters:
- con- the URLConnection to customize
- Throws:
- IOException- if thrown from URLConnection methods
 
- 
getURLThis implementation returns the underlying URL reference.- Specified by:
- getURLin interface- Resource
- Overrides:
- getURLin class- AbstractResource
 
- 
getURIThis implementation returns the underlying URI directly, if possible.- Specified by:
- getURIin interface- Resource
- Overrides:
- getURIin class- AbstractResource
- Throws:
- IOException- if the resource cannot be resolved as URI, i.e. if the resource is not available as a descriptor
 
- 
isFilepublic boolean isFile()Description copied from class:AbstractResourceThis implementation always returnsfalse.- Specified by:
- isFilein interface- Resource
- Overrides:
- isFilein class- AbstractFileResolvingResource
- See Also:
 
- 
getFileThis implementation returns a File reference for the underlying URL/URI, provided that it refers to a file in the file system.- Specified by:
- getFilein interface- Resource
- Overrides:
- getFilein class- AbstractFileResolvingResource
- Throws:
- IOException- in case of general resolution/reading failures
- See Also:
 
- 
createRelativeThis implementation creates aUrlResource, delegating tocreateRelativeURL(String)for adapting the relative path.- Specified by:
- createRelativein interface- Resource
- Overrides:
- createRelativein class- AbstractResource
- Parameters:
- relativePath- the relative path (relative to this resource)
- Returns:
- the resource handle for the relative resource
- Throws:
- MalformedURLException
- See Also:
 
- 
createRelativeURLThis delegate creates ajava.net.URL, applying the given path relative to the path of the underlying URL of this resource descriptor.A leading slash will get dropped; a "#" symbol will get encoded. Note that this method effectively cleans the combined path as of 6.1. - Throws:
- MalformedURLException
- Since:
- 5.2
- See Also:
 
- 
getFilenameThis implementation returns the URL-decoded name of the file that this URL refers to.- Specified by:
- getFilenamein interface- Resource
- Overrides:
- getFilenamein class- AbstractResource
- See Also:
 
- 
getDescriptionThis implementation returns a description that includes the URL.- See Also:
 
- 
equalsThis implementation compares the underlying URL references.- Overrides:
- equalsin class- AbstractResource
- See Also:
 
- 
hashCodepublic int hashCode()This implementation returns the hash code of the underlying URL reference.- Overrides:
- hashCodein class- AbstractResource
- See Also:
 
 
-