Class MockMultipartFile

java.lang.Object
org.springframework.mock.web.MockMultipartFile
All Implemented Interfaces:
InputStreamSource, MultipartFile

public class MockMultipartFile extends Object implements MultipartFile
Mock implementation of the MultipartFile interface.

Useful in conjunction with a MockMultipartHttpServletRequest for testing application controllers that access multipart uploads.

Since:
2.0
Author:
Juergen Hoeller, Eric Crampton
See Also:
  • Constructor Details

    • MockMultipartFile

      public MockMultipartFile(String name, @Nullable byte[] content)
      Create a new MockMultipartFile with the given content.
      Parameters:
      name - the name of the file
      content - the content of the file
    • MockMultipartFile

      public MockMultipartFile(String name, InputStream contentStream) throws IOException
      Create a new MockMultipartFile with the given content.
      Parameters:
      name - the name of the file
      contentStream - the content of the file as stream
      Throws:
      IOException - if reading from the stream failed
    • MockMultipartFile

      public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content)
      Create a new MockMultipartFile with the given content.
      Parameters:
      name - the name of the file
      originalFilename - the original filename (as on the client's machine)
      contentType - the content type (if known)
      content - the content of the file
    • MockMultipartFile

      public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream) throws IOException
      Create a new MockMultipartFile with the given content.
      Parameters:
      name - the name of the file
      originalFilename - the original filename (as on the client's machine)
      contentType - the content type (if known)
      contentStream - the content of the file as stream
      Throws:
      IOException - if reading from the stream failed
  • Method Details

    • getName

      public String getName()
      Description copied from interface: MultipartFile
      Return the name of the parameter in the multipart form.
      Specified by:
      getName in interface MultipartFile
      Returns:
      the name of the parameter (never null or empty)
    • getOriginalFilename

      @NonNull public String getOriginalFilename()
      Description copied from interface: MultipartFile
      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.

      Specified by:
      getOriginalFilename in interface MultipartFile
      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
      See Also:
    • getContentType

      @Nullable public String getContentType()
      Description copied from interface: MultipartFile
      Return the content type of the file.
      Specified by:
      getContentType in interface MultipartFile
      Returns:
      the content type, or null if not defined (or no file has been chosen in the multipart form)
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: MultipartFile
      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.
      Specified by:
      isEmpty in interface MultipartFile
    • getSize

      public long getSize()
      Description copied from interface: MultipartFile
      Return the size of the file in bytes.
      Specified by:
      getSize in interface MultipartFile
      Returns:
      the size of the file, or 0 if empty
    • getBytes

      public byte[] getBytes() throws IOException
      Description copied from interface: MultipartFile
      Return the contents of the file as an array of bytes.
      Specified by:
      getBytes in interface MultipartFile
      Returns:
      the contents of the file as bytes, or an empty byte array if empty
      Throws:
      IOException - in case of access errors (if the temporary store fails)
    • getInputStream

      public InputStream getInputStream() throws IOException
      Description copied from interface: MultipartFile
      Return an InputStream to read the contents of the file from.

      The user is responsible for closing the returned stream.

      Specified by:
      getInputStream in interface InputStreamSource
      Specified by:
      getInputStream in interface MultipartFile
      Returns:
      the contents of the file as stream, or an empty stream if empty
      Throws:
      IOException - in case of access errors (if the temporary store fails)
      See Also:
    • transferTo

      public void transferTo(File dest) throws IOException, IllegalStateException
      Description copied from interface: MultipartFile
      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.

      Specified by:
      transferTo in interface MultipartFile
      Parameters:
      dest - the destination file (typically absolute)
      Throws:
      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: