Top Description Fields Constructors Methods
org.springframework.core.io

public Class FileUrlResource

extends UrlResource
implements WritableResource
Class Inheritance
All Implemented Interfaces
org.springframework.core.io.WritableResource, org.springframework.core.io.Resource, org.springframework.core.io.InputStreamSource
Imports
java.io.File, .IOException, .OutputStream, java.net.MalformedURLException, .URL, java.nio.channels.FileChannel, .WritableByteChannel, java.nio.file.Files, .StandardOpenOption, org.springframework.lang.Nullable, org.springframework.util.ResourceUtils

Subclass of UrlResource which assumes file resolution, to the degree of implementing the WritableResource interface for it. This resource variant also caches resolved File handles from getFile().

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

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

Author
Juergen Hoeller
Since
5.0.2

Field Summary

Modifier and TypeField and Description
private volatile File

Constructor Summary

AccessConstructor and Description
public
FileUrlResource(URL
a URL
url
)

Create a new FileUrlResource based on the given URL object.

public
FileUrlResource(String
the location (i.e. the file path within that protocol)
location
)

Create a new FileUrlResource based on the given file location, using the URL protocol "file".

Method Summary

Modifier and TypeMethod and Description
public Resource
createRelative(String
the relative path (relative to this resource)
relativePath
)

Overrides org.springframework.core.io.UrlResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

Create a resource relative to this resource.
public File
getFile()

Overrides org.springframework.core.io.UrlResource.getFile.

Implements org.springframework.core.io.Resource.getFile.

Return a File handle for this resource.
public OutputStream
getOutputStream()

Implements org.springframework.core.io.WritableResource.getOutputStream.

Return an OutputStream for the underlying resource, allowing to (over-)write its content.
public boolean
isWritable()

Overrides default org.springframework.core.io.WritableResource.isWritable.

Indicate whether the contents of this resource can be written via getOutputStream().
public WritableByteChannel
Inherited from org.springframework.core.io.UrlResource:
createRelativeURLcustomizeConnectionequalsfromfromgetDescriptiongetFilenamegetInputStreamgetURIgetURLhashCodeisFile

Field Detail

fileback to summary
private volatile File file
Annotations
@Nullable

Constructor Detail

FileUrlResourceback to summary
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:URL

a URL

See Also
ResourceUtils#isFileURL(URL), getFile()
FileUrlResourceback to summary
public FileUrlResource(String location) throws MalformedURLException

Create a new FileUrlResource based on the given file location, using the URL protocol "file".

The given parts will automatically get encoded if necessary.

Parameters
location:String

the location (i.e. the file path within that protocol)

Exceptions
MalformedURLException:
if the given URL specification is not valid
See Also
UrlResource#UrlResource(String, String), ResourceUtils#URL_PROTOCOL_FILE

Method Detail

createRelativeback to summary
public Resource createRelative(String relativePath) throws MalformedURLException

Overrides org.springframework.core.io.UrlResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

Doc from org.springframework.core.io.Resource.createRelative.

Create a resource relative to this resource.

Parameters
relativePath:String

the relative path (relative to this resource)

Returns:Resource

the resource handle for the relative resource

Annotations
@Override
getFileback to summary
public File getFile() throws IOException

Overrides org.springframework.core.io.UrlResource.getFile.

Implements org.springframework.core.io.Resource.getFile.

Doc from org.springframework.core.io.Resource.getFile.

Return a File handle for this resource.

Annotations
@Override
Exceptions
IOException:
in case of general resolution/reading failures
getOutputStreamback to summary
public OutputStream getOutputStream() throws IOException

Implements org.springframework.core.io.WritableResource.getOutputStream.

Doc from org.springframework.core.io.WritableResource.getOutputStream.

Return an OutputStream for the underlying resource, allowing to (over-)write its content.

Annotations
@Override
Exceptions
IOException:
if the stream could not be opened
isWritableback to summary
public boolean isWritable()

Overrides default org.springframework.core.io.WritableResource.isWritable.

Doc from org.springframework.core.io.WritableResource.isWritable.

Indicate whether the contents of this resource can be written via getOutputStream().

Will be true for typical resource descriptors; note that actual content writing may still fail when attempted. However, a value of false is a definitive indication that the resource content cannot be modified.

Annotations
@Override
writableChannelback to summary
public WritableByteChannel writableChannel() throws IOException

Overrides default org.springframework.core.io.WritableResource.writableChannel.

Doc from org.springframework.core.io.WritableResource.writableChannel.

Return a WritableByteChannel.

It is expected that each call creates a fresh channel.

The default implementation returns Channels#newChannel(OutputStream) with the result of getOutputStream().

Returns:WritableByteChannel

the byte channel for the underlying resource (must not be null)

Annotations
@Override
Exceptions
IOException:
if the content channel could not be opened