Using RequestParam for multipartfile is a right way?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Yes, using @RequestParam for MultipartFile is the standard and correct approach in Spring Boot for handling file uploads. The annotation binds a multipart form field to a MultipartFile parameter in your controller method. For more complex scenarios with additional metadata, use @RequestPart instead. Both annotations work with multipart/form-data requests and are fully supported by Spring's multipart resolver.
Basic File Upload with @RequestParam
The client sends a multipart/form-data request with a form field named "file".
cURL Test
Multiple File Upload
File Upload with Additional Form Data
@RequestParam vs @RequestPart
@RequestPart uses the Content-Type header of each part for deserialization. Use it when sending JSON alongside files:
File Size Configuration
File Validation
Saving to Different Locations
Exception Handling
Common Pitfalls
- Missing
multipart/form-datacontent type: The client must send the request withContent-Type: multipart/form-data. Sending JSON with a file field does not work — the file must be a multipart form part. Most HTTP clients (Postman, cURL with-F) set this automatically. - Parameter name mismatch:
@RequestParam("file")must match the form field name exactly. If the client sends[email protected]but the annotation says@RequestParam("document"), Spring returns a 400 error with "Required request part 'document' is not present". - File size limit exceeded silently: The default
max-file-sizeis 1MB. Large uploads fail with aMaxUploadSizeExceededException. Configurespring.servlet.multipart.max-file-sizeandmax-request-sizein application properties. - Using
@RequestBodyinstead of@RequestParam:@RequestBodyreads the entire request body as a single object (for JSON). For file uploads, use@RequestParamor@RequestPart. Using@RequestBodywithMultipartFilecauses a "Content type not supported" error. - Not sanitizing file names:
file.getOriginalFilename()returns the client-provided filename, which may contain path traversal characters like../. Always sanitize the filename before saving:Paths.get(filename).getFileName().toString().
Summary
@RequestParam("file") MultipartFile fileis the standard approach for file uploads in Spring Boot- Use
@RequestPartwhen combining files with JSON metadata in the same request - Configure
max-file-sizeandmax-request-sizein application properties for large uploads - Validate file size, content type, and file extension before processing
- Always sanitize
getOriginalFilename()to prevent path traversal attacks - Handle
MaxUploadSizeExceededExceptionwith a@ControllerAdvicefor user-friendly error messages
Related reading
- Using Schema Registry from Confluent with Avro and Kafka in Spring Boot Applications
- Using sping''s restTemplate with a timeout, how do I detect a timeout?
- Using Spring Boot together with gRPC and Protobuf
- Using Spring Boot's ErrorController and Spring's ResponseEntityExceptionHandler correctly
- Using spring embedded ldap to simulate active directory for integration tests
- Using spring HATEOAS with spring webflux Functional Web Framework reactor-netty
- Using Spring threading and TaskExecutor, how do I know when a thread is finished?
- Using ssl certificate with feign

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.