All Implemented Interfaces:
InputStreamSource, Resource
Direct Known Subclasses:
FileUrlResource

public class UrlResource extends AbstractFileResolvingResource
{link 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 java.net.URL
  • Constructor Details

    • UrlResource

      public UrlResource(URL url)
      Create a new UrlResource based on the given URL object.
      Parameters:
      url - a URL see #UrlResource(URI) see #UrlResource(String)
    • UrlResource

      public UrlResource(String path) throws MalformedURLException
      Create a new UrlResource 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 java.net.URL#URL(String)
  • Method Details

    • 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. see java.net.URL#openConnection() see java.net.URLConnection#setUseCaches(boolean) see java.net.URLConnection#getInputStream()

      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 Resource#isReadable()
    • 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
    • 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. see org.springframework.util.ResourceUtils#getFile(java.net.URL, String)
      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 #getInputStream()
    • createRelative

      public Resource createRelative(String relativePath) throws MalformedURLException
      This implementation creates a UrlResource, delegating to {link #createRelativeURL(String)} for adapting the relative path. see #createRelativeURL(String)
      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
    • 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. see #createRelative(String) see java.net.URL#URL(java.net.URL, String)
      Throws:
      MalformedURLException
      Since:
      5.2
    • getFilename

      public String getFilename()
      This implementation returns the name of the file that this URL refers to. see java.net.URL#getPath()
      Specified by:
      getFilename in interface Resource
      Overrides:
      getFilename in class AbstractResource
    • getDescription

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

      public boolean equals(@Nullable Object other)
      This implementation compares the underlying URL references.
      Overrides:
      equals in class AbstractResource
    • hashCode

      public int hashCode()
      This implementation returns the hash code of the underlying URL reference.
      Overrides:
      hashCode in class AbstractResource