Top Description Methods
org.springframework.web.multipart

public Interface MultipartFile

extends InputStreamSource
Known Direct Implementers
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile
Imports
java.io.File, .IOException, .InputStream, java.nio.file.Files, .Path, org.springframework.core.io.InputStreamSource, .Resource, org.springframework.lang.Nullable, org.springframework.util.FileCopyUtils

A representation of an uploaded file received in a multipart request.

The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storage will be cleared at the end of request processing.

Authors
Juergen Hoeller, Trevor D. Cook
Since
29.09.2003
See Also
org.springframework.web.multipart.MultipartHttpServletRequest, org.springframework.web.multipart.MultipartResolver

Method Summary

Modifier and TypeMethod and Description
public byte[]

Returns:

the contents of the file as bytes, or an empty byte array if empty
getBytes
()

Return the contents of the file as an array of bytes.

public String

Returns:

the content type, or null if not defined (or no file has been chosen in the multipart form)
getContentType
()

Return the content type of the file.

public InputStream

Returns:

the contents of the file as stream, or an empty stream if empty
getInputStream
()

Redeclares org.springframework.core.io.InputStreamSource.getInputStream.

Return an InputStream to read the contents of the file from.

public String

Returns:

the name of the parameter (never null or empty)
getName
()

Return the name of the parameter in the multipart form.

public String

Returns:

the original filename, or the empty String if no file has been chosen in the multipart form, or null if not defined or not available
getOriginalFilename
()

Return the original filename in the client's filesystem.

public default Resource

Returns:

this MultipartFile adapted to the Resource contract
getResource
()

Return a Resource representation of this MultipartFile.

public long

Returns:

the size of the file, or 0 if empty
getSize
()

Return the size of the file in bytes.

public boolean
isEmpty()

Return whether the uploaded file is empty, that is, either no file has been chosen in the multipart form or the chosen file has no content.

public void
transferTo(File
the destination file (typically absolute)
dest
)

Transfer the received file to the given destination file.

public default void
transferTo(Path dest)

Transfer the received file to the given destination file.

Method Detail

getBytesback to summary
public byte[] getBytes() throws IOException

Return the contents of the file as an array of bytes.

Returns:byte[]

the contents of the file as bytes, or an empty byte array if empty

Exceptions
IOException:
in case of access errors (if the temporary store fails)
getContentTypeback to summary
public String getContentType()

Return the content type of the file.

Returns:String

the content type, or null if not defined (or no file has been chosen in the multipart form)

Annotations
@Nullable
getInputStreamback to summary
public InputStream getInputStream() throws IOException

Redeclares org.springframework.core.io.InputStreamSource.getInputStream.

Return an InputStream to read the contents of the file from.

The user is responsible for closing the returned stream.

Returns:InputStream

the contents of the file as stream, or an empty stream if empty

Annotations
@Override
Exceptions
IOException:
in case of access errors (if the temporary store fails)
getNameback to summary
public String getName()

Return the name of the parameter in the multipart form.

Returns:String

the name of the parameter (never null or empty)

getOriginalFilenameback to summary
public String getOriginalFilename()

Return the original filename in the client's filesystem.

This may contain path information depending on the browser used, but it typically will not with any other than Opera.

Note

Please keep in mind this filename is supplied by the client and should not be used blindly. In addition to not using the directory portion, the file name could also contain characters such as ".." and others that can be used maliciously. It is recommended to not use this filename directly. Preferably generate a unique one and save this one somewhere for reference, if necessary.

Returns:String

the original filename, or the empty String if no file has been chosen in the multipart form, or null if not defined or not available

Annotations
@Nullable
See Also
RFC 7578, Section 4.2, Unrestricted File Upload
getResourceback to summary
public default Resource getResource()

Return a Resource representation of this MultipartFile. This can be used as input to the RestTemplate or the WebClient to expose content length and the filename along with the InputStream.

Returns:Resource

this MultipartFile adapted to the Resource contract

Since
5.1
getSizeback to summary
public long getSize()

Return the size of the file in bytes.

Returns:long

the size of the file, or 0 if empty

isEmptyback to summary
public boolean isEmpty()

Return whether the uploaded file is empty, that is, either no file has been chosen in the multipart form or the chosen file has no content.

transferToback to summary
public void transferTo(File dest) throws IOException, IllegalStateException

Transfer the received file to the given destination file.

This may either move the file in the filesystem, copy the file in the filesystem, or save memory-held contents to the destination file. If the destination file already exists, it will be deleted first.

If the target file has been moved in the filesystem, this operation cannot be invoked again afterwards. Therefore, call this method just once in order to work with any storage mechanism.

Note

Depending on the underlying provider, temporary storage may be container-dependent, including the base directory for relative destinations specified here (e.g. with Servlet multipart handling). For absolute destinations, the target file may get renamed/moved from its temporary location or newly copied, even if a temporary copy already exists.

Parameters
dest:File

the destination file (typically absolute)

Exceptions
IOException:
in case of reading or writing errors
IllegalStateException:
if the file has already been moved in the filesystem and is not available anymore for another transfer
See Also
jakarta.servlet.http.Part#write(String)
transferToback to summary
public default void transferTo(Path dest) throws IOException, IllegalStateException

Transfer the received file to the given destination file.

The default implementation simply copies the file input stream.

Since
5.1
See Also
getInputStream(), transferTo(File)