In a file upload endpoint, @RequestParam binds one part of a multipart/form-data request to a controller parameter. When the parameter type is MultipartFile, Spring hands you the uploaded file, and the annotation's name must match the form field name. Plain text fields in the same form can be bound with @RequestParam too.
@RequestParam works well for files plus simple values. When one part carries JSON that should be deserialized into an object, use @RequestPart instead.
The form field name and the annotation value must match. In the HTML form below the field is called file, so the controller parameter is bound with @RequestParam("file").
The controller stores the file under a generated name instead of the client-supplied one. That choice is deliberate and covered in the security section below.
| Method | Purpose |
|---|---|
getOriginalFilename() | The name sent by the client. Treat it as untrusted. |
getContentType() | The content type declared by the client. Also untrusted. |
getSize() | Size in bytes. |
isEmpty() | True when no file was chosen or the file has no content. |
getInputStream() | Stream the content without loading it all in memory. |
getBytes() | The whole content as a byte array. Avoid it for large files. |
transferTo(Path) | Moves or copies the upload to a destination. |
transferTo(Path) is the shortest way to save a file, but the underlying temporary file may be moved, so you can only call it once for a given upload in some implementations. Streaming with getInputStream() is safe to use in all cases.
Bind multiple files with a List<MultipartFile> or an array. All parts must use the same field name.
In HTML, add the multiple attribute to the input: <input type="file" name="files" multiple>. If you need every part regardless of name, bind a Map<String, MultipartFile> or a MultiValueMap<String, MultipartFile>.
@RequestParam is required by default. If the part is missing, Spring throws MissingServletRequestParameterException, or MissingServletRequestPartException for multipart parts in some cases, and the response is 400 Bad Request.
@RequestParam(required = false) MultipartFile file gives null when the part is absent.@RequestParam Optional<MultipartFile> file gives an empty Optional.Note that a form submitted without choosing a file still sends the part with an empty filename, so check file.isEmpty() as well as null.
@RequestParam | @RequestPart | |
|---|---|---|
| Binds | A request parameter or a file part | A named part of a multipart request |
| Text value conversion | Uses type conversion (like a query parameter) | Uses message converters (like @RequestBody) |
| JSON part into an object | No | Yes |
When a client sends a file plus JSON metadata, use @RequestPart so the JSON part is deserialized with Jackson. The JSON part needs its own Content-Type: application/json.
Spring Boot limits uploads by default: a single file is limited to 1MB and a whole request to 10MB. Change them in application.properties:
When a limit is exceeded, Spring raises MaxUploadSizeExceededException. Handle it in a @RestControllerAdvice and return a clear message, ideally with HTTP 413. Your reverse proxy (for example Nginx with client_max_body_size) may reject large uploads before they reach the application, so set matching limits there too.
getOriginalFilename() as a path. It can contain ../ or other tricks. Generate your own name, or normalise the value and check that the final path stays inside the upload directory.getContentType(). Inspect the content (magic bytes) or use a scanning library when file type matters.Usually the form lacks enctype="multipart/form-data", the field name differs from the annotation value, or the client sent the file with a different part name. Compile with -parameters, or always give the name explicitly.
Use @RequestParam for a file and simple text fields. Use @RequestPart when a part contains JSON or another structured body that a message converter must parse.
Increase spring.servlet.multipart.max-file-size and spring.servlet.multipart.max-request-size. Also check the limits of any proxy or load balancer in front of the app.
Yes. Call getInputStream() and copy the stream to its destination instead of loading all bytes at once.
@RequestParam binds a multipart file part to a MultipartFile parameter by field name, and it also binds text fields in the same form. Use List<MultipartFile> for several files, required = false for optional ones, and @RequestPart when a part is JSON. Set explicit size limits and never trust the client filename or content type.