In Spring, @RequestParam is how a controller reads a named part of a multipart request. For file uploads, it maps a form field to a MultipartFile, so the uploaded file arrives as an object you can validate, stream and store. The annotation matters because it ties the HTTP multipart format to ordinary Java method parameters.
This article focuses on the design side: why you bind files this way, how to receive several files and metadata together, how to validate them, and how to avoid loading large files into memory.
A file upload is a multipart/form-data request. Each field, including each file, is a separate part with its own headers. Spring parses the body once, then makes each part available by name. @RequestParam is the simplest binding for those parts, because it needs no message converter, only a name and a type.
The example streams the file with getInputStream() instead of getBytes(), so memory use stays flat for large uploads.
Because text fields are request parameters too, a single method can take both.
A curl call that exercises it:
Bind a list when the client sends many files under the same field name.
In HTML, use <input type="file" name="files" multiple>. A Map<String, MultipartFile> or MultipartHttpServletRequest is available when field names are not known in advance.
Validate on the server, because client-side checks are only a convenience.
| Check | How |
|---|---|
| Not empty | file.isEmpty() |
| Maximum size | spring.servlet.multipart.max-file-size plus an explicit getSize() check for business rules |
| Allowed type | Compare getContentType() to an allow-list such as image/png and image/jpeg |
| Safe name | Ignore getOriginalFilename() for storage, generate your own name |
| Content match | Read the first bytes (magic numbers) when the type matters for security |
Spring Boot defaults to 1 MB per file and 10 MB per request. Change them with properties:
file-size-threshold controls when Boot writes the part to disk rather than keeping it in memory. If a limit is exceeded, MaxUploadSizeExceededException is thrown, and a missing part raises MissingServletRequestPartException or MissingServletRequestParameterException. Handle them in a @RestControllerAdvice to return clear 400 or 413 responses.
@RequestParam treats a non-file part as a string. If a part contains JSON, such as a metadata object, it will not be deserialised for you. Use @RequestPart("meta") @Valid Metadata meta for that, because @RequestPart applies an HttpMessageConverter based on the part's Content-Type. For very large files or resumable uploads, consider direct-to-object-storage uploads with presigned URLs, so the file does not pass through your application.
Use MultipartFile in Spring MVC. In reactive Spring WebFlux, the equivalent is FilePart bound with @RequestPart, because the Servlet multipart API is not used there.
Set required = false, or declare Optional<MultipartFile>. The parameter is then null or empty when the client sends no file.
No. It comes from the client and can contain path segments or odd characters. Generate your own name and resolve it inside a fixed directory.
That is Spring Boot's default max-file-size. Change spring.servlet.multipart.max-file-size and max-request-size.
@RequestParam gives Spring controllers a direct, type-safe way to read multipart parts, especially MultipartFile. Combine it with text fields, bind lists for multiple files, validate size and type on the server, and stream to storage. Switch to @RequestPart when a part is JSON that needs conversion and validation.