Class PathResource
- java.lang.Object
- org.springframework.core.io.AbstractResource
- org.springframework.core.io.PathResource
- All Implemented Interfaces:
InputStreamSource,Resource,WritableResource
@UsesJava7 public class PathResource extends AbstractResource implements WritableResource
Resourceimplementation forjava.nio.file.Pathhandles. Supports resolution as File, and also as URL. Implements the extendedWritableResourceinterface.- Since:
- 4.0
- Author:
- Philippe Marschall, Juergen Hoeller
- See Also:
Path,Files,FileSystemResource
Constructor Summary
Constructors Constructor Description PathResource(String path)Create a new PathResource from a Path handle.PathResource(URI uri)Create a new PathResource from a Path handle.PathResource(Path path)Create a new PathResource from a Path handle.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longcontentLength()This implementation returns the underlying file's length.ResourcecreateRelative(String relativePath)This implementation creates a PathResource, applying the given path relative to the path of the underlying file of this resource descriptor.booleanequals(Object obj)This implementation compares the underlying Path references.booleanexists()This implementation returns whether the underlying file exists.StringgetDescription()Return a description for this resource, to be used for error output when working with the resource.FilegetFile()This implementation returns the underlying File reference.StringgetFilename()This implementation returns the name of the file.InputStreamgetInputStream()This implementation opens a InputStream for the underlying file.OutputStreamgetOutputStream()This implementation opens a OutputStream for the underlying file.StringgetPath()Return the file path for this resource.URIgetURI()This implementation returns a URI for the underlying file.URLgetURL()This implementation returns a URL for the underlying file.inthashCode()This implementation returns the hash code of the underlying Path reference.booleanisReadable()This implementation checks whether the underlying file is marked as readable (and corresponds to an actual file with content, not to a directory).booleanisWritable()This implementation checks whether the underlying file is marked as writable (and corresponds to an actual file with content, not to a directory).longlastModified()This implementation returns the underlying File's timestamp.Methods inherited from class org.springframework.core.io.AbstractResource
getFileForLastModifiedCheck, isOpen, toString
Constructor Detail
PathResource
public PathResource(Path path)
Create a new PathResource from a Path handle.Note: Unlike
FileSystemResource, when building relative resources viacreateRelative(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(String path)
Create a new PathResource from a Path handle.Note: Unlike
FileSystemResource, when building relative resources viacreateRelative(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(URI uri)
Create a new PathResource from a Path handle.Note: Unlike
FileSystemResource, when building relative resources viacreateRelative(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
exists
public boolean exists()
This implementation returns whether the underlying file exists.- Specified by:
existsin interfaceResource- Overrides:
existsin classAbstractResource- See Also:
Files.exists(Path, java.nio.file.LinkOption...)
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:
isReadablein interfaceResource- Overrides:
isReadablein classAbstractResource- See Also:
Files.isReadable(Path),Files.isDirectory(Path, java.nio.file.LinkOption...)
getInputStream
public InputStream getInputStream() throws IOException
This implementation opens a InputStream for the underlying file.- Specified by:
getInputStreamin interfaceInputStreamSource- Returns:
- the input stream for the underlying resource (must not be
null) - Throws:
FileNotFoundException- if the underlying resource doesn't existIOException- 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:
isWritablein interfaceWritableResource- See Also:
Files.isWritable(Path),Files.isDirectory(Path, java.nio.file.LinkOption...)
getOutputStream
public OutputStream getOutputStream() throws IOException
This implementation opens a OutputStream for the underlying file.- Specified by:
getOutputStreamin interfaceWritableResource- Throws:
IOException- if the stream could not be opened- See Also:
FileSystemProvider.newOutputStream(Path, OpenOption...)
getURL
public URL getURL() throws IOException
This implementation returns a URL for the underlying file.- Specified by:
getURLin interfaceResource- Overrides:
getURLin classAbstractResource- Throws:
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 URI getURI() throws IOException
This implementation returns a URI for the underlying file.- Specified by:
getURIin interfaceResource- Overrides:
getURIin classAbstractResource- Throws:
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 File getFile() throws IOException
This implementation returns the underlying File reference.- Specified by:
getFilein interfaceResource- Overrides:
getFilein classAbstractResource- Throws:
FileNotFoundException- if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file systemIOException- in case of general resolution/reading failures- See Also:
InputStreamSource.getInputStream()
contentLength
public long contentLength() throws IOException
This implementation returns the underlying file's length.- Specified by:
contentLengthin interfaceResource- Overrides:
contentLengthin classAbstractResource- Throws:
IOException- if the resource cannot be resolved (in the file system or as some other known physical resource type)- See Also:
InputStreamSource.getInputStream()
lastModified
public long lastModified() throws IOException
This implementation returns the underlying File's timestamp.- Specified by:
lastModifiedin interfaceResource- Overrides:
lastModifiedin classAbstractResource- Throws:
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(String relativePath) throws IOException
This implementation creates a PathResource, applying the given path relative to the path of the underlying file of this resource descriptor.- Specified by:
createRelativein interfaceResource- Overrides:
createRelativein classAbstractResource- Parameters:
relativePath- the relative path (relative to this resource)- Returns:
- the resource handle for the relative resource
- Throws:
IOException- if the relative resource cannot be determined- See Also:
Path.resolve(String)
getFilename
public String getFilename()
This implementation returns the name of the file.- Specified by:
getFilenamein interfaceResource- Overrides:
getFilenamein classAbstractResource- See Also:
Path.getFileName()
getDescription
public String getDescription()
Description copied from interface:ResourceReturn 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
toStringmethod.- Specified by:
getDescriptionin interfaceResource- See Also:
Object.toString()
equals
public boolean equals(Object obj)
This implementation compares the underlying Path references.- Overrides:
equalsin classAbstractResource- See Also:
Resource.getDescription()
hashCode
public int hashCode()
This implementation returns the hash code of the underlying Path reference.- Overrides:
hashCodein classAbstractResource- See Also:
Resource.getDescription()