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`

public class UrlResource extends AbstractFileResolvingResource
`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:
  - `URL`

## Constructor Summary

Constructors
Constructor
Description
`UrlResource(String path)`
Create a new `UrlResource` based on a URI path.
`UrlResource(String protocol, String location)`
Create a new `UrlResource` based on a URI specification.
`UrlResource(String protocol, String location, String fragment)`
Create a new `UrlResource` based on a URI specification.
`UrlResource(URI uri)`
Create a new `UrlResource` based on the given URI object.
`UrlResource(URL url)`
Create a new `UrlResource` based on the given URL object.

## Method Summary

Modifier and Type
Method
Description
`Resource`
`createRelative(String relativePath)`
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 con)`
Customize the given `URLConnection` before fetching the resource.
`boolean`
`equals(Object other)`
This implementation compares the underlying URL references.
`static UrlResource`
`from(String path)`
Create a new `UrlResource` from the given URL path.
`static UrlResource`
`from(URI uri)`
Create a new `UrlResource` from the given `URI`.
`String`
`getDescription()`
This implementation returns a description that includes the URL.
`File`
`getFile()`
This implementation returns a File reference for the underlying URL/URI, provided that it refers to a file in the file system.
`String`
`getFilename()`
This implementation returns the URL-decoded name of the file that this URL refers to.
`InputStream`
`getInputStream()`
This implementation opens an InputStream for the given URL.
`URI`
`getURI()`
This implementation returns the underlying URI directly, if possible.
`URL`
`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 returns `false`.

### Methods inherited from class org.springframework.core.io.AbstractFileResolvingResource

`contentLength, customizeConnection, exists, getFile, getFileForLastModifiedCheck, isFile, isReadable, lastModified, readableChannel`

### Methods inherited from class org.springframework.core.io.AbstractResource

`isOpen, toString`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.springframework.core.io.Resource

`getContentAsByteArray, getContentAsString`

## Constructor Details

### UrlResource

public UrlResource(URL url)
Create a new `UrlResource` based on the given URL object.

Parameters:
  `url` - a URL
See Also:
  - `UrlResource(URI)`
  - `UrlResource(String)`

### UrlResource

public UrlResource(URI uri) throws MalformedURLException
Create a new `UrlResource` based 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(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` - a URI path
Throws:
  `MalformedURLException` - if the given URI path is not valid
See Also:
  - `ResourceUtils.toURI(String)`

### UrlResource

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` - 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:
  - `URI(String, String, String)`

### UrlResource

public UrlResource(String protocol, String location, @Nullable String fragment) throws MalformedURLException
Create a new `UrlResource` based 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:
  - `URI(String, String, String)`

## Method Details

### from

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 `Stream` and `Optional` APIs or other scenarios when a checked `IOException` is undesirable.

Parameters:
  `uri` - a URI
Throws:
  `UncheckedIOException` - if the given URL path is not valid
Since:
  6.0
See Also:
  - `UrlResource(URI)`

### from

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 `Stream` and `Optional` APIs or other scenarios when a checked `IOException` is undesirable.

Parameters:
  `path` - a URL path
Throws:
  `UncheckedIOException` - if the given URL path is not valid
Since:
  6.0
See Also:
  - `UrlResource(String)`

### getInputStream

public InputStream getInputStream() throws IOException
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:
  the input stream for the underlying resource (must not be `null`)
Throws:
  `FileNotFoundException` - if the underlying resource does not exist
  `IOException` - if the content stream could not be opened
See Also:
  - `URL.openConnection()`
  - `URLConnection.setUseCaches(boolean)`
  - `URLConnection.getInputStream()`

### customizeConnection

protected void customizeConnection(URLConnection con) throws IOException
Description copied from class: `AbstractFileResolvingResource`
Customize the given `URLConnection` before fetching the resource.

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

Overrides:
  `customizeConnection` in class `AbstractFileResolvingResource`
Parameters:
  `con` - the URLConnection to customize
Throws:
  `IOException` - if thrown from URLConnection methods

### getURL

public URL getURL()
This implementation returns the underlying URL reference.

Specified by:
  `getURL` in interface `Resource`
Overrides:
  `getURL` in class `AbstractResource`

### getURI

public URI getURI() throws IOException
This implementation returns the underlying URI directly, if possible.

Specified by:
  `getURI` in interface `Resource`
Overrides:
  `getURI` in class `AbstractResource`
Throws:
  `IOException` - if the resource cannot be resolved as URI, i.e. if the resource is not available as a descriptor

### isFile

public boolean isFile()
Description copied from class: `AbstractResource`
This implementation always returns `false`.

Specified by:
  `isFile` in interface `Resource`
Overrides:
  `isFile` in class `AbstractFileResolvingResource`
See Also:
  - `Resource.getFile()`

### 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:
  `getFile` in interface `Resource`
Overrides:
  `getFile` in class `AbstractFileResolvingResource`
Throws:
  `FileNotFoundException` - if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
  `IOException` - 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 a `UrlResource`, delegating to `createRelativeURL(String)` for adapting the relative path.

Specified by:
  `createRelative` in interface `Resource`
Overrides:
  `createRelative` in class `AbstractResource`
Parameters:
  `relativePath` - the relative path (relative to this resource)
Returns:
  the resource handle for the relative resource
Throws:
  `MalformedURLException`
See Also:
  - `createRelativeURL(String)`

### createRelativeURL

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. Note that this method effectively cleans the combined path as of 6.1.

Throws:
  `MalformedURLException`
Since:
  5.2
See Also:
  - `createRelative(String)`
  - `ResourceUtils.toRelativeURL(URL, String)`

### getFilename

@Nullable public String getFilename()
This implementation returns the URL-decoded name of the file that this URL refers to.

Specified by:
  `getFilename` in interface `Resource`
Overrides:
  `getFilename` in class `AbstractResource`
See Also:
  - `URL.getPath()`
  - `URLDecoder.decode(String, java.nio.charset.Charset)`

### getDescription

public String getDescription()
This implementation returns a description that includes the URL.

See Also:
  - `Object.toString()`

### equals

public boolean equals(@Nullable Object other)
This implementation compares the underlying URL references.

Overrides:
  `equals` in class `AbstractResource`
See Also:
  - `Resource.getDescription()`

### hashCode

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

Overrides:
  `hashCode` in class `AbstractResource`
See Also:
  - `Resource.getDescription()`