Top Description Fields Constructors Methods
org.springframework.core.io

public Class UrlResource

extends AbstractFileResolvingResource
Class Inheritance
Known Direct Subclasses
org.springframework.core.io.FileUrlResource
Imports
java.io.File, .IOException, .InputStream, .UncheckedIOException, java.net.HttpURLConnection, .MalformedURLException, .URI, .URISyntaxException, .URL, .URLConnection, .URLDecoder, java.nio.charset.StandardCharsets, java.util.Base64, org.springframework.lang.Nullable, org.springframework.util.Assert, .ResourceUtils, .StringUtils

Resource implementation for java.net.URL locators. Supports resolution as a URL and also as a File in case of the "file:" protocol.
Authors
Juergen Hoeller, Sam Brannen
Since
28.12.2003
See Also
java.net.URL

Field Summary

Modifier and TypeField and Description
private static final String
private volatile String
cleanedUrl

Cleaned URL String (with normalized path), used for comparisons.

private final URI
uri

Original URI, if available; used for URI and File access.

private final URL
url

Original URL, used for actual access.

Constructor Summary

AccessConstructor and Description
public
UrlResource(URL
a URL
url
)

Create a new UrlResource based on the given URL object.

public
UrlResource(URI
a URI
uri
)

Create a new UrlResource based on the given URI object.

public
UrlResource(String
a URI path
path
)

Create a new UrlResource based on a URI path.

public
UrlResource(String
the URL protocol to use (e.g. "jar" or "file" - without colon); also known as "scheme"
protocol
,
String
the location (e.g. the file path within that protocol); also known as "scheme-specific part"
location
)

Create a new UrlResource based on a URI specification.

public
UrlResource(String
the URL protocol to use (e.g. "jar" or "file" - without colon); also known as "scheme"
protocol
,
String
the location (e.g. the file path within that protocol); also known as "scheme-specific part"
location
,
String
the fragment within that location (e.g. anchor on an HTML page, as following after a "#" separator)
fragment
)

Create a new UrlResource based on a URI specification.

Method Summary

Modifier and TypeMethod and Description
public Resource
createRelative(String
the relative path (relative to this resource)
relativePath
)

Overrides org.springframework.core.io.AbstractResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

This implementation creates a UrlResource, delegating to createRelativeURL(String) for adapting the relative path.
protected URL
createRelativeURL(String relativePath)

This delegate creates a java.net.URL, applying the given path relative to the path of the underlying URL of this resource descriptor.

protected void
customizeConnection(URLConnection
the URLConnection to customize
con
)

Overrides org.springframework.core.io.AbstractFileResolvingResource.customizeConnection.

Customize the given URLConnection before fetching the resource.
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides org.springframework.core.io.AbstractResource.equals.

This implementation compares the underlying URL references.
public static UrlResource
from(URI
a URI
uri
)

Create a new UrlResource from the given URI.

public static UrlResource
from(String
a URL path
path
)

Create a new UrlResource from the given URL path.

private String
getCleanedUrl()

Lazily determine a cleaned URL for the given original URL.

public String
getDescription()

Implements org.springframework.core.io.Resource.getDescription.

This implementation returns a description that includes the URL.
public File
getFile()

Overrides org.springframework.core.io.AbstractFileResolvingResource.getFile.

Implements org.springframework.core.io.Resource.getFile.

This implementation returns a File reference for the underlying URL/URI, provided that it refers to a file in the file system.
public String
getFilename()

Overrides org.springframework.core.io.AbstractResource.getFilename.

Implements org.springframework.core.io.Resource.getFilename.

This implementation returns the URL-decoded name of the file that this URL refers to.
public InputStream
getInputStream()

Implements org.springframework.core.io.InputStreamSource.getInputStream.

This implementation opens an InputStream for the given URL.
public URI
getURI()

Overrides org.springframework.core.io.AbstractResource.getURI.

Implements org.springframework.core.io.Resource.getURI.

This implementation returns the underlying URI directly, if possible.
public URL
getURL()

Overrides org.springframework.core.io.AbstractResource.getURL.

Implements org.springframework.core.io.Resource.getURL.

This implementation returns the underlying URL reference.
public int
hashCode()

Overrides org.springframework.core.io.AbstractResource.hashCode.

This implementation returns the hash code of the underlying URL reference.
public boolean
isFile()

Overrides org.springframework.core.io.AbstractFileResolvingResource.isFile.

Overrides default org.springframework.core.io.Resource.isFile.

This implementation always returns false.
Inherited from org.springframework.core.io.AbstractFileResolvingResource:
checkReadablecontentLengthcustomizeConnectionexistsgetFilegetFileForLastModifiedCheckisFileisReadablelastModifiedreadableChannel

Field Detail

AUTHORIZATIONback to summary
private static final String AUTHORIZATION
cleanedUrlback to summary
private volatile String cleanedUrl

Cleaned URL String (with normalized path), used for comparisons.

Annotations
@Nullable
uriback to summary
private final URI uri

Original URI, if available; used for URI and File access.

Annotations
@Nullable
urlback to summary
private final URL url

Original URL, used for actual access.

Constructor Detail

UrlResourceback to summary
public UrlResource(URL url)

Create a new UrlResource based on the given URL object.

Parameters
url:URL

a URL

See Also
UrlResource(URI), UrlResource(String)
UrlResourceback to summary
public UrlResource(URI uri) throws MalformedURLException

Create a new UrlResource based on the given URI object.

Parameters
uri:URI

a URI

Exceptions
MalformedURLException:
if the given URL path is not valid
Since
2.5
UrlResourceback to summary
public UrlResource(String path) throws MalformedURLException

Create a new UrlResource based on a URI path.

Note

The given path needs to be pre-encoded if necessary.

Parameters
path:String

a URI path

Exceptions
MalformedURLException:
if the given URI path is not valid
See Also
ResourceUtils#toURI(String)
UrlResourceback to summary
public UrlResource(String protocol, String location) throws MalformedURLException

Create a new UrlResource based on a URI specification.

The given parts will automatically get encoded if necessary.

Parameters
protocol:String

the URL protocol to use (e.g. "jar" or "file" - without colon); also known as "scheme"

location:String

the location (e.g. the file path within that protocol); also known as "scheme-specific part"

Exceptions
MalformedURLException:
if the given URL specification is not valid
See Also
java.net.URI#URI(String, String, String)
UrlResourceback to summary
public UrlResource(String protocol, String location, String fragment) throws MalformedURLException

Create a new UrlResource based on a URI specification.

The given parts will automatically get encoded if necessary.

Parameters
protocol:String

the URL protocol to use (e.g. "jar" or "file" - without colon); also known as "scheme"

location:String

the location (e.g. the file path within that protocol); also known as "scheme-specific part"

fragment:String

the fragment within that location (e.g. anchor on an HTML page, as following after a "#" separator)

Exceptions
MalformedURLException:
if the given URL specification is not valid
See Also
java.net.URI#URI(String, String, String)

Method Detail

createRelativeback to summary
public Resource createRelative(String relativePath) throws MalformedURLException

Overrides org.springframework.core.io.AbstractResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

This implementation creates a UrlResource, delegating to createRelativeURL(String) for adapting the relative path.

Parameters
relativePath:String

Doc from org.springframework.core.io.Resource.createRelative.

the relative path (relative to this resource)

Returns:Resource

Doc from org.springframework.core.io.Resource.createRelative.

the resource handle for the relative resource

Annotations
@Override
See Also
createRelativeURL(String)
createRelativeURLback to summary
protected URL createRelativeURL(String relativePath) throws MalformedURLException

This delegate creates a java.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.

Since
5.2
See Also
createRelative(String), ResourceUtils#toRelativeURL(URL, String)
customizeConnectionback to summary
protected void customizeConnection(URLConnection con) throws IOException

Overrides org.springframework.core.io.AbstractFileResolvingResource.customizeConnection.

Doc from org.springframework.core.io.AbstractFileResolvingResource.customizeConnection.

Customize the given URLConnection before fetching the resource.

Calls ResourceUtils#useCachesIfNecessary(URLConnection) and delegates to customizeConnection(HttpURLConnection) if possible. Can be overridden in subclasses.

Parameters
con:URLConnection

the URLConnection to customize

Annotations
@Override
Exceptions
IOException:
if thrown from URLConnection methods
equalsback to summary
public boolean equals(Object other)

Overrides org.springframework.core.io.AbstractResource.equals.

This implementation compares the underlying URL references.

Parameters
other:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
fromback to summary
public static UrlResource from(URI uri) throws UncheckedIOException

Create a new UrlResource from the given URI.

This factory method is a convenience for UrlResource(URI) that catches any MalformedURLException and rethrows it wrapped in an UncheckedIOException; suitable for use in java.util.stream.Stream and java.util.Optional APIs or other scenarios when a checked IOException is undesirable.

Parameters
uri:URI

a URI

Exceptions
UncheckedIOException:
if the given URL path is not valid
Since
6.0
See Also
UrlResource(URI)
fromback to summary
public static UrlResource from(String path) throws UncheckedIOException

Create a new UrlResource from the given URL path.

This factory method is a convenience for UrlResource(String) that catches any MalformedURLException and rethrows it wrapped in an UncheckedIOException; suitable for use in java.util.stream.Stream and java.util.Optional APIs or other scenarios when a checked IOException is undesirable.

Parameters
path:String

a URL path

Exceptions
UncheckedIOException:
if the given URL path is not valid
Since
6.0
See Also
UrlResource(String)
getCleanedUrlback to summary
private String getCleanedUrl()

Lazily determine a cleaned URL for the given original URL.

getDescriptionback to summary
public String getDescription()

Implements org.springframework.core.io.Resource.getDescription.

This implementation returns a description that includes the URL.

Annotations
@Override
getFileback to summary
public File getFile() throws IOException

Overrides org.springframework.core.io.AbstractFileResolvingResource.getFile.

Implements org.springframework.core.io.Resource.getFile.

This implementation returns a File reference for the underlying URL/URI, provided that it refers to a file in the file system.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.getFile.

in case of general resolution/reading failures

See Also
org.springframework.util.ResourceUtils#getFile(java.net.URL, String)
getFilenameback to summary
public String getFilename()

Overrides org.springframework.core.io.AbstractResource.getFilename.

Implements org.springframework.core.io.Resource.getFilename.

This implementation returns the URL-decoded name of the file that this URL refers to.

Annotations
@Override
@Nullable
See Also
java.net.URL#getPath(), java.net.URLDecoder#decode(String, java.nio.charset.Charset)
getInputStreamback to summary
public InputStream getInputStream() throws IOException

Implements org.springframework.core.io.InputStreamSource.getInputStream.

This implementation opens an InputStream for the given URL.

It sets the useCaches flag to false, mainly to avoid jar file locking on Windows.

Returns:InputStream

Doc from org.springframework.core.io.InputStreamSource.getInputStream.

the input stream for the underlying resource (must not be null)

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.InputStreamSource.getInputStream.

if the content stream could not be opened

See Also
java.net.URL#openConnection(), java.net.URLConnection#setUseCaches(boolean), java.net.URLConnection#getInputStream()
getURIback to summary
public URI getURI() throws IOException

Overrides org.springframework.core.io.AbstractResource.getURI.

Implements org.springframework.core.io.Resource.getURI.

This implementation returns the underlying URI directly, if possible.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.getURI.

if the resource cannot be resolved as URI, i.e. if the resource is not available as a descriptor

getURLback to summary
public URL getURL()

Overrides org.springframework.core.io.AbstractResource.getURL.

Implements org.springframework.core.io.Resource.getURL.

This implementation returns the underlying URL reference.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides org.springframework.core.io.AbstractResource.hashCode.

This implementation returns the hash code of the underlying URL reference.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override
isFileback to summary
public boolean isFile()

Overrides org.springframework.core.io.AbstractFileResolvingResource.isFile.

Overrides default org.springframework.core.io.Resource.isFile.

Doc from org.springframework.core.io.AbstractResource.isFile.

This implementation always returns false.

Annotations
@Override