MultipartFile is Spring's abstraction for a file uploaded in a multipart/form-data request. It gives your controller the file name, content type, size and content through a small interface, so you never parse the multipart stream yourself. Spring binds each uploaded part to a MultipartFile parameter when you annotate it with @RequestParam or @RequestPart.
The interface lives in org.springframework.web.multipart and works the same way in Spring MVC and Spring Boot. The Servlet container does the low-level parsing, and Spring wraps each part for you.
A multipart request carries several parts: text fields and one or more files. For every file part, Spring creates a MultipartFile that holds its metadata and gives you access to its bytes. The data may be kept in memory or in a temporary file, depending on size, and Spring cleans temporary files up after the request.
The methods you use most:
| Method | Purpose |
|---|---|
getName() | Name of the form field (the part name) |
getOriginalFilename() | File name sent by the client; never trust it |
getContentType() | Content type declared by the client; never trust it either |
getSize() | Size in bytes |
isEmpty() | True when no file was chosen or the file has no content |
getBytes() | Whole content as a byte[], which loads it into memory |
getInputStream() | Stream the content without loading it all |
transferTo(Path) | Move or copy the content to a destination on disk |
The example below accepts one file, validates it, and stores it under a server-generated name.
You can send a test request with curl:
The transferTo(Path) overload has been available since Spring 5.1. Pass an absolute, already-resolved Path so the destination does not depend on the container's temporary storage location.
Declare a List<MultipartFile> (or MultipartFile[]) to accept repeated parts with the same field name:
To receive a file together with structured data, use @RequestPart. Spring converts the JSON part with an HttpMessageConverter, so the part needs its own application/json content type:
Spring Boot configures multipart handling automatically. The default limits are small (1 MB per file, 10 MB per request), so most applications raise them:
file-size-threshold is the size above which Spring writes the upload to disk instead of keeping it in memory. When a limit is exceeded, Spring throws MaxUploadSizeExceededException. Handle it in a @RestControllerAdvice and return 413 Payload Too Large with a clear message.
The client controls the file name, content type and content, so treat all three as untrusted input.
getOriginalFilename(). A value such as ../../etc/cron.d/job can escape your upload directory (path traversal). Generate the stored name yourself and keep the original only as metadata.getContentType(). Check the real content, for example by reading the leading bytes (magic numbers) or using a content-detection library.Content-Type and Content-Disposition.Use MockMultipartFile to simulate an upload without a running server:
@RequestParam works for simple file parts and text fields. @RequestPart uses message converters, so it is the right choice when a part contains JSON or XML that you want bound to an object.
The file or the whole request is larger than spring.servlet.multipart.max-file-size or max-request-size. Raise the limits, and check any proxy or gateway in front of the app, since Nginx and similar servers have their own body-size limits.
Use getInputStream() or transferTo() for anything that may be large. getBytes() loads the entire file into memory, which is fine only for small files.
No. WebFlux uses FilePart from org.springframework.http.codec.multipart and non-blocking streams instead of MultipartFile.
MultipartFile turns an uploaded part of a multipart request into a simple object with metadata and content access. Bind it with @RequestParam or @RequestPart, set realistic size limits, store files under names you generate, validate the real content, and prefer streaming or transferTo() over getBytes() for large files.