Package org.springframework.core.io
Class UrlResource
java.lang.Object
org.springframework.core.io.AbstractResource
org.springframework.core.io.AbstractFileResolvingResource
org.springframework.core.io.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
- See Also:
-
Constructor Summary
ConstructorDescriptionUrlResource
(String path) Create a newUrlResource
based on a URL path.UrlResource
(String protocol, String location) Create a newUrlResource
based on a URI specification.UrlResource
(String protocol, String location, String fragment) Create a newUrlResource
based on a URI specification.UrlResource
(URI uri) Create a newUrlResource
based on the given URI object.UrlResource
(URL url) Create a newUrlResource
based on the given URL object. -
Method Summary
Modifier and TypeMethodDescriptioncreateRelative
(String relativePath) This implementation creates aUrlResource
, delegating tocreateRelativeURL(String)
for adapting the relative path.protected URL
createRelativeURL
(String relativePath) This delegate creates ajava.net.URL
, applying the given path relative to the path of the underlying URL of this resource descriptor.boolean
This implementation compares the underlying URL references.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 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.int
hashCode()
This implementation returns the hash code of the underlying URL reference.boolean
isFile()
This implementation always returnsfalse
.Methods inherited from class org.springframework.core.io.AbstractFileResolvingResource
contentLength, customizeConnection, customizeConnection, exists, getFile, getFileForLastModifiedCheck, isFile, isReadable, lastModified, readableChannel
Methods inherited from class org.springframework.core.io.AbstractResource
isOpen, toString
-
Constructor Details
-
UrlResource
Create a newUrlResource
based on the given URI object.- Parameters:
uri
- a URI- Throws:
MalformedURLException
- if the given URL path is not valid- Since:
- 2.5
-
UrlResource
Create a newUrlResource
based on the given URL object.- Parameters:
url
- a URL
-
UrlResource
Create a newUrlResource
based on a URL path.Note: The given path needs to be pre-encoded if necessary.
- Parameters:
path
- a URL path- Throws:
MalformedURLException
- if the given URL path is not valid- See Also:
-
UrlResource
Create a newUrlResource
based on a URI specification.The given parts will automatically get encoded if necessary.
- Parameters:
protocol
- the URL protocol to use (e.g. "jar" or "file" - without colon); also known as "scheme"location
- the location (e.g. the file path within that protocol); also known as "scheme-specific part"- Throws:
MalformedURLException
- if the given URL specification is not valid- See Also:
-
UrlResource
public UrlResource(String protocol, String location, @Nullable String fragment) throws MalformedURLException Create a newUrlResource
based on a URI specification.The given parts will automatically get encoded if necessary.
- Parameters:
protocol
- the URL protocol to use (e.g. "jar" or "file" - without colon); also known as "scheme"location
- the location (e.g. the file path within that protocol); also known as "scheme-specific part"fragment
- the fragment within that location (e.g. anchor on an HTML page, as following after a "#" separator)- Throws:
MalformedURLException
- if the given URL specification is not valid- See Also:
-
-
Method Details
-
getInputStream
This implementation opens an InputStream for the given URL.It sets the
useCaches
flag tofalse
, mainly to avoid jar file locking on Windows.- Returns:
- the input stream for the underlying resource (must not be
null
) - Throws:
FileNotFoundException
- if the underlying resource does not existIOException
- if the content stream could not be opened- See Also:
-
getURL
This implementation returns the underlying URL reference.- Specified by:
getURL
in interfaceResource
- Overrides:
getURL
in classAbstractResource
-
getURI
This implementation returns the underlying URI directly, if possible.- Specified by:
getURI
in interfaceResource
- Overrides:
getURI
in classAbstractResource
- Throws:
IOException
- if the resource cannot be resolved as URI, i.e. if the resource is not available as descriptor
-
isFile
public boolean isFile()Description copied from class:AbstractResource
This implementation always returnsfalse
.- Specified by:
isFile
in interfaceResource
- Overrides:
isFile
in classAbstractFileResolvingResource
- See Also:
-
getFile
This implementation returns a File reference for the underlying URL/URI, provided that it refers to a file in the file system.- Specified by:
getFile
in interfaceResource
- Overrides:
getFile
in classAbstractFileResolvingResource
- Throws:
FileNotFoundException
- if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file systemIOException
- in case of general resolution/reading failures- See Also:
-
createRelative
This implementation creates aUrlResource
, delegating tocreateRelativeURL(String)
for adapting the relative path.- Specified by:
createRelative
in interfaceResource
- Overrides:
createRelative
in classAbstractResource
- Parameters:
relativePath
- the relative path (relative to this resource)- Returns:
- the resource handle for the relative resource
- Throws:
MalformedURLException
- See Also:
-
createRelativeURL
This 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.- Throws:
MalformedURLException
- Since:
- 5.2
- See Also:
-
getFilename
This implementation returns the name of the file that this URL refers to.- Specified by:
getFilename
in interfaceResource
- Overrides:
getFilename
in classAbstractResource
- See Also:
-
getDescription
This implementation returns a description that includes the URL.- See Also:
-
equals
This implementation compares the underlying URL references.- Overrides:
equals
in classAbstractResource
- See Also:
-
hashCode
public int hashCode()This implementation returns the hash code of the underlying URL reference.- Overrides:
hashCode
in classAbstractResource
- See Also:
-