Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What is the role of @RequestParam in file uploads?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 30, 2024
UpdatedNov 16, 2026
6 min read

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.

Table of Contents

  • Receiving a single file with @RequestParam
  • What MultipartFile gives you
  • Uploading several files
  • Required, optional and missing parameters
  • @RequestParam versus @RequestPart
  • Configuring upload size limits
  • Securing file uploads
  • Frequently Asked Questions
    • Why is my @RequestParam MultipartFile null or missing?
    • Should I use @RequestParam or @RequestPart for files?
    • How do I upload a file larger than 1MB in Spring Boot?
    • Can I read the file as a stream?
  • Summary

Receiving a single file with @RequestParam

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").

html
java

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.

What MultipartFile gives you

MethodPurpose
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.

Uploading several files

Bind multiple files with a List<MultipartFile> or an array. All parts must use the same field name.

java

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>.

Required, optional and missing parameters

@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 versus @RequestPart

@RequestParam@RequestPart
BindsA request parameter or a file partA named part of a multipart request
Text value conversionUses type conversion (like a query parameter)Uses message converters (like @RequestBody)
JSON part into an objectNoYes

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.

java

Configuring upload size limits

Spring Boot limits uploads by default: a single file is limited to 1MB and a whole request to 10MB. Change them in application.properties:

text

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.

Securing file uploads

  • Never use 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.
  • Do not trust getContentType(). Inspect the content (magic bytes) or use a scanning library when file type matters.
  • Store uploads outside the web root, or in object storage such as S3, and serve them through a controller.
  • Set size limits, and require authentication for the endpoint.
  • Add CSRF protection for browser forms, or send tokens with your API calls.

Frequently Asked Questions

Why is my @RequestParam MultipartFile null or missing?

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.

Should I use @RequestParam or @RequestPart for files?

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.

How do I upload a file larger than 1MB in Spring Boot?

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.

Can I read the file as a stream?

Yes. Call getInputStream() and copy the stream to its destination instead of loading all bytes at once.

Summary

@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.

Share