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
public class UrlResource extends AbstractFileResolvingResource
Resourceimplementation forjava.net.URLlocators. Supports resolution as aURLand also as aFilein case of the"file:"protocol.- Since:
- 28.12.2003
- Author:
- Juergen Hoeller
- See Also:
URL
Constructor Summary
Constructors Constructor Description UrlResource(String path)Create a newUrlResourcebased on a URL path.UrlResource(String protocol, String location)Create a newUrlResourcebased on a URI specification.UrlResource(String protocol, String location, 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 Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ResourcecreateRelative(String relativePath)This implementation creates aUrlResource, applying the given path relative to the path of the underlying URL of this resource descriptor.booleanequals(Object obj)This implementation compares the underlying URL references.StringgetDescription()This implementation returns a description that includes the URL.FilegetFile()This implementation returns a File reference for the underlying URL/URI, provided that it refers to a file in the file system.StringgetFilename()This implementation returns the name of the file that this URL refers to.InputStreamgetInputStream()This implementation opens an InputStream for the given URL.URIgetURI()This implementation returns the underlying URI directly, if possible.URLgetURL()This implementation returns the underlying URL reference.inthashCode()This implementation returns the hash code of the underlying URL reference.Methods inherited from class org.springframework.core.io.AbstractFileResolvingResource
contentLength, customizeConnection, customizeConnection, exists, getFile, getFileForLastModifiedCheck, isReadable, lastModified
Methods inherited from class org.springframework.core.io.AbstractResource
isOpen, toString
Constructor Detail
UrlResource
public UrlResource(URI uri) throws MalformedURLException
Create a newUrlResourcebased on the given URI object.- Parameters:
uri- a URI- Throws:
MalformedURLException- if the given URL path is not valid- Since:
- 2.5
UrlResource
public UrlResource(URL url)
Create a newUrlResourcebased on the given URL object.- Parameters:
url- a URL
UrlResource
public UrlResource(String path) throws MalformedURLException
Create a newUrlResourcebased 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:
URL(String)
UrlResource
public UrlResource(String protocol, String location) 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 (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:
URI(String, String, String)
UrlResource
public UrlResource(String protocol, String location, 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 (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:
URI(String, String, String)
Method Detail
getInputStream
public InputStream getInputStream() throws IOException
This 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:
FileNotFoundException- if the underlying resource doesn't existIOException- if the content stream could not be opened- See Also:
URL.openConnection(),URLConnection.setUseCaches(boolean),URLConnection.getInputStream()
getURL
public URL getURL() throws IOException
This implementation returns the underlying URL reference.- Specified by:
getURLin interfaceResource- Overrides:
getURLin classAbstractResource- Throws:
IOException- if the resource cannot be resolved as URL, i.e. if the resource is not available as descriptor
getURI
public URI getURI() throws IOException
This implementation returns the underlying URI directly, if possible.- Specified by:
getURIin interfaceResource- Overrides:
getURIin classAbstractResource- Throws:
IOException- if the resource cannot be resolved as URI, i.e. if the resource is not available as descriptor
getFile
public File getFile() throws IOException
This implementation returns a File reference for the underlying URL/URI, provided that it refers to a file in the file system.- Specified by:
getFilein interfaceResource- Overrides:
getFilein 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:
ResourceUtils.getFile(java.net.URL, String)
createRelative
public Resource createRelative(String relativePath) throws MalformedURLException
This implementation creates aUrlResource, applying the given path relative to the path of the underlying URL of this resource descriptor.- Specified by:
createRelativein interfaceResource- Overrides:
createRelativein classAbstractResource- Parameters:
relativePath- the relative path (relative to this resource)- Returns:
- the resource handle for the relative resource
- Throws:
MalformedURLException- See Also:
URL(java.net.URL, String)
getFilename
public String getFilename()
This implementation returns the name of the file that this URL refers to.- Specified by:
getFilenamein interfaceResource- Overrides:
getFilenamein classAbstractResource- See Also:
URL.getPath()
getDescription
public String getDescription()
This implementation returns a description that includes the URL.- See Also:
Object.toString()
equals
public boolean equals(Object obj)
This implementation compares the underlying URL references.- Overrides:
equalsin classAbstractResource- See Also:
Resource.getDescription()
hashCode
public int hashCode()
This implementation returns the hash code of the underlying URL reference.- Overrides:
hashCodein classAbstractResource- See Also:
Resource.getDescription()