In Spring MVC and Spring Boot, @RequestParam binds a part of a multipart/form-data request to a controller parameter. When the parameter type is MultipartFile, Spring hands you the uploaded file, with its name, size, content type and bytes. The name in the annotation must match the name attribute of the form field that carries the file.
This is the simplest way to accept a file, and it works well when the request has a file plus a few text fields. When one part is a JSON document, @RequestPart is a better fit.
A browser sends a file as one part of a multipart body. Spring's MultipartResolver (standard Servlet multipart support in Boot) parses the body, and @RequestParam looks up the part by name.
The matching HTML form must use POST and enctype="multipart/form-data":
Here name="file" matches @RequestParam("file"). Without the enctype, the browser sends only the file name and Spring cannot find a part.
| Method | Returns |
|---|---|
getOriginalFilename() | Name supplied by the client, never trust it as a path |
getContentType() | Content type declared by the client |
getSize() | Size in bytes |
getInputStream() | Stream to read without loading everything in memory |
getBytes() | Whole content as a byte array, avoid for large files |
transferTo(Path) | Writes the upload to a destination |
isEmpty() | True when no file was chosen or it has no content |
The client controls the file name and content type, so treat both as untrusted input. Generate your own file name, and resolve it inside a fixed directory.
Check the content type against an allow-list, and for sensitive cases inspect the real content rather than relying on the declared type. Never serve uploaded files from a directory that can execute code.
Because every multipart part is a request parameter, you can mix files and text fields in the same method.
Make the file optional with required = false. The parameter is then null when the part is absent. You can also declare it as Optional<MultipartFile>.
Boot limits uploads by default: 1 MB per file and 10 MB per request. Raise them in application.properties:
When a limit is exceeded, Spring throws MaxUploadSizeExceededException. Handle it to return a clear message:
Also check limits on the proxy or load balancer in front of the app, such as client_max_body_size in Nginx, because they can reject a request before Spring sees it.
With MockMvc you can build a multipart request without a browser:
You can also test it from the command line with curl -F "[email protected]" http://localhost:8080/upload.
Yes. Declare @RequestParam("files") List<MultipartFile> files, or use MultipartFile[]. The form input needs the multiple attribute and every file must use the same field name.
Usually the form is missing enctype="multipart/form-data", or the part name does not match the annotation value. A size limit can also cause rejection before your method runs.
Use @RequestPart when a part contains JSON or XML that should be converted to an object, or when you want validation of that part. For a plain file and text fields, @RequestParam is enough.
Spring Boot 3 and Spring Framework 6 use jakarta.servlet, but MultipartFile and @RequestParam are unchanged, so your controller code stays the same.
@RequestParam("file") MultipartFile file is the standard way to receive an uploaded file in Spring. Match the form field name, use multipart/form-data, set size limits, and save files under generated names in a fixed directory. Handle MaxUploadSizeExceededException and use @RequestPart when a part is structured JSON.