Explain how Spring MVC supports file upload.

Spring MVC supports file uploads through the MultipartFile abstraction, which represents an uploaded file as a bound method parameter once the underlying multipart request has been parsed by a multipart resolver.

Key Points: • The HTML form must set enctype="multipart/form-data" so the browser sends the file as part of a multipart request body. • @RequestParam binds a MultipartFile parameter to the name attribute of the file input on the form. • MultipartFile exposes methods like getBytes(), getOriginalFilename(), getSize(), and transferTo() for working with the uploaded content. • Spring Boot auto-configures a StandardServletMultipartResolver, so no manual bean registration is needed in most cases. • Multiple files can be uploaded at once by binding to a List<MultipartFile> parameter with matching input names.

Example: A profile picture upload form posts to /uploadAvatar with an input named "file"; the controller method declares @RequestParam("file") MultipartFile file, checks its size and content type, and calls file.transferTo(destination) to save it to disk.

Code Example:

@PostMapping("/uploadAvatar")
public String upload(@RequestParam("file") MultipartFile file) throws IOException {
    if (!file.isEmpty()) {
        file.transferTo(new File("/uploads/" + file.getOriginalFilename()));
    }
    return "redirect:/profile";
}

Interview Tip: A concise interview answer is:

"Spring MVC represents an uploaded file as a MultipartFile bound via @RequestParam, as long as the form uses enctype multipart/form-data. From there I can read the file's bytes, name, and size, or call transferTo() to save it — Spring Boot wires up the multipart resolver automatically."