org.springframework.core.io

Class PathResource

    • Constructor Summary

      Constructors 
      Constructor and Description
      PathResource(java.nio.file.Path path)
      Create a new PathResource from a Path handle.
      PathResource(java.lang.String path)
      Create a new PathResource from a Path handle.
      PathResource(java.net.URI uri)
      Create a new PathResource from a Path handle.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      long contentLength()
      This implementation returns the underlying file's length.
      Resource createRelative(java.lang.String relativePath)
      This implementation creates a PathResource, applying the given path relative to the path of the underlying file of this resource descriptor.
      boolean equals(java.lang.Object other)
      This implementation compares the underlying Path references.
      boolean exists()
      This implementation returns whether the underlying file exists.
      java.lang.String getDescription()
      Return a description for this resource, to be used for error output when working with the resource.
      java.io.File getFile()
      This implementation returns the underlying File reference.
      java.lang.String getFilename()
      This implementation returns the name of the file.
      java.io.InputStream getInputStream()
      This implementation opens a InputStream for the underlying file.
      java.io.OutputStream getOutputStream()
      This implementation opens a OutputStream for the underlying file.
      java.lang.String getPath()
      Return the file path for this resource.
      java.net.URI getURI()
      This implementation returns a URI for the underlying file.
      java.net.URL getURL()
      This implementation returns a URL for the underlying file.
      int hashCode()
      This implementation returns the hash code of the underlying Path reference.
      boolean isFile()
      This implementation always indicates a file.
      boolean isReadable()
      This implementation checks whether the underlying file is marked as readable (and corresponds to an actual file with content, not to a directory).
      boolean isWritable()
      This implementation checks whether the underlying file is marked as writable (and corresponds to an actual file with content, not to a directory).
      long lastModified()
      This implementation returns the underlying File's timestamp.
      java.nio.channels.ReadableByteChannel readableChannel()
      This implementation opens a Channel for the underlying file.
      java.nio.channels.WritableByteChannel writableChannel()
      This implementation opens a Channel for the underlying file.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface org.springframework.core.io.Resource

        isOpen
    • Constructor Detail

      • PathResource

        public PathResource(java.nio.file.Path path)
        Create a new PathResource from a Path handle.

        Note: Unlike FileSystemResource, when building relative resources via createRelative(java.lang.String), the relative path will be built underneath the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" -> "C:/dir1/dir2"!

        Parameters:
        path - a Path handle
      • PathResource

        public PathResource(java.lang.String path)
        Create a new PathResource from a Path handle.

        Note: Unlike FileSystemResource, when building relative resources via createRelative(java.lang.String), the relative path will be built underneath the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" -> "C:/dir1/dir2"!

        Parameters:
        path - a path
        See Also:
        Paths.get(String, String...)
      • PathResource

        public PathResource(java.net.URI uri)
        Create a new PathResource from a Path handle.

        Note: Unlike FileSystemResource, when building relative resources via createRelative(java.lang.String), the relative path will be built underneath the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" -> "C:/dir1/dir2"!

        Parameters:
        uri - a path URI
        See Also:
        Paths.get(URI)
    • Method Detail

      • getPath

        public final java.lang.String getPath()
        Return the file path for this resource.
      • isReadable

        public boolean isReadable()
        This implementation checks whether the underlying file is marked as readable (and corresponds to an actual file with content, not to a directory).
        Specified by:
        isReadable in interface Resource
        Overrides:
        isReadable in class AbstractResource
        See Also:
        Files.isReadable(Path), Files.isDirectory(Path, java.nio.file.LinkOption...)
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.IOException
        This implementation opens a InputStream for the underlying file.
        Specified by:
        getInputStream in interface InputStreamSource
        Returns:
        the input stream for the underlying resource (must not be null)
        Throws:
        java.io.FileNotFoundException - if the underlying resource doesn't exist
        java.io.IOException - if the content stream could not be opened
        See Also:
        FileSystemProvider.newInputStream(Path, OpenOption...)
      • isWritable

        public boolean isWritable()
        This implementation checks whether the underlying file is marked as writable (and corresponds to an actual file with content, not to a directory).
        Specified by:
        isWritable in interface WritableResource
        See Also:
        Files.isWritable(Path), Files.isDirectory(Path, java.nio.file.LinkOption...)
      • getOutputStream

        public java.io.OutputStream getOutputStream()
                                             throws java.io.IOException
        This implementation opens a OutputStream for the underlying file.
        Specified by:
        getOutputStream in interface WritableResource
        Throws:
        java.io.IOException - if the stream could not be opened
        See Also:
        FileSystemProvider.newOutputStream(Path, OpenOption...)
      • getURL

        public java.net.URL getURL()
                            throws java.io.IOException
        This implementation returns a URL for the underlying file.
        Specified by:
        getURL in interface Resource
        Overrides:
        getURL in class AbstractResource
        Throws:
        java.io.IOException - if the resource cannot be resolved as URL, i.e. if the resource is not available as descriptor
        See Also:
        Path.toUri(), URI.toURL()
      • getURI

        public java.net.URI getURI()
                            throws java.io.IOException
        This implementation returns a URI for the underlying file.
        Specified by:
        getURI in interface Resource
        Overrides:
        getURI in class AbstractResource
        Throws:
        java.io.IOException - if the resource cannot be resolved as URI, i.e. if the resource is not available as descriptor
        See Also:
        Path.toUri()
      • getFile

        public java.io.File getFile()
                             throws java.io.IOException
        This implementation returns the underlying File reference.
        Specified by:
        getFile in interface Resource
        Overrides:
        getFile in class AbstractResource
        Throws:
        java.io.FileNotFoundException - if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
        java.io.IOException - in case of general resolution/reading failures
        See Also:
        InputStreamSource.getInputStream()
      • readableChannel

        public java.nio.channels.ReadableByteChannel readableChannel()
                                                              throws java.io.IOException
        This implementation opens a Channel for the underlying file.
        Specified by:
        readableChannel in interface Resource
        Overrides:
        readableChannel in class AbstractResource
        Returns:
        the byte channel for the underlying resource (must not be null)
        Throws:
        java.io.FileNotFoundException - if the underlying resource doesn't exist
        java.io.IOException - if the content channel could not be opened
        See Also:
        Files.newByteChannel(Path, OpenOption...)
      • writableChannel

        public java.nio.channels.WritableByteChannel writableChannel()
                                                              throws java.io.IOException
        This implementation opens a Channel for the underlying file.
        Specified by:
        writableChannel in interface WritableResource
        Returns:
        the byte channel for the underlying resource (must not be null)
        Throws:
        java.io.FileNotFoundException - if the underlying resource doesn't exist
        java.io.IOException - if the content channel could not be opened
        See Also:
        Files.newByteChannel(Path, OpenOption...)
      • lastModified

        public long lastModified()
                          throws java.io.IOException
        This implementation returns the underlying File's timestamp.
        Specified by:
        lastModified in interface Resource
        Overrides:
        lastModified in class AbstractResource
        Throws:
        java.io.IOException - if the resource cannot be resolved (in the file system or as some other known physical resource type)
        See Also:
        Files.getLastModifiedTime(Path, java.nio.file.LinkOption...)
      • createRelative

        public Resource createRelative(java.lang.String relativePath)
                                throws java.io.IOException
        This implementation creates a PathResource, applying the given path relative to the path of the underlying file of this resource descriptor.
        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:
        java.io.IOException - if the relative resource cannot be determined
        See Also:
        Path.resolve(String)
      • getFilename

        public java.lang.String getFilename()
        This implementation returns the name of the file.
        Specified by:
        getFilename in interface Resource
        Overrides:
        getFilename in class AbstractResource
        See Also:
        Path.getFileName()
      • getDescription

        public java.lang.String getDescription()
        Description copied from interface: Resource
        Return a description for this resource, to be used for error output when working with the resource.

        Implementations are also encouraged to return this value from their toString method.

        Specified by:
        getDescription in interface Resource
        See Also:
        Object.toString()