All Implemented Interfaces:
InputStreamSource, Resource

public class FileUrlResource extends UrlResource implements Resource
Subclass of {link UrlResource} which assumes file resolution, to the degree of implementing the {link WritableResource} interface for it. This resource variant also caches resolved {link File} handles from {link #getFile()}.

This is the class resolved by {link DefaultResourceLoader} for a "file:..." URL location, allowing a downcast to {link WritableResource} for it.

Alternatively, for direct construction from a {link java.io.File} handle or NIO {link java.nio.file.Path}, consider using {link FileSystemResource}.

Since:
5.0.2
Author:
Juergen Hoeller
  • Constructor Details

    • FileUrlResource

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

      Note that this does not enforce "file" as URL protocol. If a protocol is known to be resolvable to a file, it is acceptable for this purpose.

      Parameters:
      url - a URL see ResourceUtils#isFileURL(URL) see #getFile()
  • Method Details

    • getFile

      public File getFile() throws IOException
      Description copied from class: UrlResource
      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 UrlResource
      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
      Description copied from class: UrlResource
      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 UrlResource
      Parameters:
      relativePath - the relative path (relative to this resource)
      Returns:
      the resource handle for the relative resource
      Throws:
      MalformedURLException