File uploads in Spring Boot are typically handled by a @RestController using MultipartFile parameters, though a plain @Controller with @ResponseBody offers more control when the response needs custom headers or streamed content.
Key Points: • @RestController is simplest for uploads where the response is just JSON metadata (like a file ID and status). • Spring binds multipart form fields directly to a MultipartFile parameter, giving access to the file's bytes, name, and content type. • @RestController can complicate scenarios where you need fine-grained control over response headers, like streaming a file back or setting a custom Content-Disposition. • In that case, @Controller with @ResponseBody on individual methods, or returning a ResponseEntity<Resource>, gives more explicit control over the HTTP response. • spring.servlet.multipart.max-file-size and max-request-size must be configured to allow files above the small default limits.
Example: An upload endpoint that just accepts a profile picture and returns {"status":"ok"} works fine as a @RestController, but an endpoint that must stream a large file back with a specific Content-Disposition header is easier to control returning a ResponseEntity<Resource> explicitly.
Code Example:
@PostMapping("/upload")
public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) {
storageService.store(file);
return ResponseEntity.ok("uploaded: " + file.getOriginalFilename());
}Interview Tip: A concise interview answer is:
"For most uploads where the response is just JSON status, @RestController with a MultipartFile parameter is the simplest choice. It gets awkward when I need precise control over response headers or streamed file content back to the client -- there I'd use ResponseEntity<Resource> or a plain @Controller for finer-grained control."