Q1. How do you handle file upload in Spring MVC?
Configure
In controller, use
Example:
MultipartResolver (e.g., CommonsMultipartResolver or StandardServletMultipartResolver).In controller, use
@RequestParam("file") MultipartFile file.Example:
@PostMapping("/upload")
public String handleUpload(@RequestParam MultipartFile file) {
file.transferTo(new File("/tmp/" + file.getOriginalFilename()));
return "success";
}
Q2. What is MultipartFile?
It provides methods to get file name, size, content type, and to transfer to a file or get bytes.
It's part of Spring's file upload support.
MultipartFile is an interface representing an uploaded file.It provides methods to get file name, size, content type, and to transfer to a file or get bytes.
It's part of Spring's file upload support.
Q3. How do you configure max file size?
In Servlet 3.0, you can set
In Spring Boot, set properties:
In XML config, you can set
multipart-config in web.xml or via MultipartConfigElement.In Spring Boot, set properties:
spring.servlet.multipart.max-file-size, max-request-size.In XML config, you can set
maxUploadSize on CommonsMultipartResolver.Q4. How do you handle multiple file uploads?
Use
Example:
@RequestParam with MultipartFile array or List.Example:
@PostMapping("/upload")
public String uploadMultiple(@RequestParam MultipartFile[] files) { ... }
Q5. What is the difference between CommonsMultipartResolver and StandardServletMultipartResolver?
The latter is preferred for Servlet 3.0+ containers.
Both are configured with bean definitions.
CommonsMultipartResolver uses Apache Commons FileUpload, while StandardServletMultipartResolver uses Servlet 3.0's built-in multipart support.The latter is preferred for Servlet 3.0+ containers.
Both are configured with bean definitions.
