MultipartFile is the Spring interface that represents a file received in a multipart/form-data request. It gives you the original file name, content type, size and content as bytes or a stream, and it lets you store the file with transferTo. Spring MVC and Spring Boot bind it to a controller parameter, so you handle uploads without parsing the request yourself.
Because the file name and content type come from the client, treat them as untrusted input and validate them before saving anything.
When a browser or API client posts a form with enctype="multipart/form-data", Spring's MultipartResolver parses the request and wraps each file part in a MultipartFile. In Spring Boot, the StandardServletMultipartResolver is auto-configured. Reactive WebFlux applications use a different type, FilePart, not MultipartFile.
| Method | Returns |
|---|---|
getName() | The name of the form field, for example file. |
getOriginalFilename() | The file name supplied by the client. Never trust it as a path. |
getContentType() | The content type sent by the client, such as image/png. It can be spoofed. |
getSize() | The size in bytes. |
isEmpty() | True when no file was chosen or the file has no content. |
getBytes() | The whole content in memory. Avoid for large files. |
getInputStream() | A stream to read or copy the content. |
transferTo(Path) | Moves or copies the upload to a destination. |
The controller rejects empty files and types that are not allowed. It generates a unique stored name, strips directory parts from the client name, and checks that the final path stays inside the upload directory, which prevents path traversal such as ../../etc/passwd. Note that getContentType() is client supplied, so for sensitive systems also inspect the file content (for example with Apache Tika) and scan for malware.
Bind a list or array to accept multiple files in one field:
To combine a file with JSON metadata, use @RequestPart("meta") UploadMeta meta next to the MultipartFile parameter.
Spring Boot limits uploads by default, and exceeding the limit throws MaxUploadSizeExceededException.
file-size-threshold is the size above which Spring writes the upload to temporary disk storage instead of keeping it in memory. Also set limits in your reverse proxy, such as client_max_body_size in Nginx. Handle the exception in a @ControllerAdvice and return HTTP 413.
MockMultipartFile lets you simulate uploads in MockMvc tests without a real browser.
getName() is the form field name, while getOriginalFilename() is the name of the file on the client machine.
No. WebFlux uses FilePart and Part from org.springframework.http.codec.multipart.
Read the content with getInputStream() and pass it to the storage client. Do not use getBytes() for large files, because it loads everything into memory.
No. It is sent by the client. Validate the real content and allow only the types you need.
MultipartFile is Spring's abstraction for uploaded files. Bind it with @RequestParam or @RequestPart, validate emptiness, type and size, sanitise the file name, store with transferTo or a stream, and configure upload limits. Use FilePart instead in reactive applications.