Close

Spring MVC - Understanding MultipartResolver

[Last Updated: Feb 2, 2018]

Spring uses strategy interface MultipartResolver for multipart file upload resolution.

Following is MultipartResolver snippet:

 package org.springframework.web.multipart;
 ......
 public interface MultipartResolver {
	boolean isMultipart(HttpServletRequest request);
	MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException;
	void cleanupMultipart(MultipartHttpServletRequest request);
}

DispatcherServlet looks for an instance of MultipartResolver registered as a bean by the name of DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME ("multipartResolver"). No default bean is registered. That means, to enable file uploading, the application has to register an implementation of MultipartResolver.

The DispatcherServlet.doDispatch() method checks if the request is for file upload by calling MultipartResolver.isMultipart(). This method returns true if the request is typically a POST request with the content-type of "multipart/form-data". On passing this check, the method MultipartResolver#resolveMultipart() is called which parses the given HTTP request into multipart files and parameters, and wraps the request inside a MultipartHttpServletRequest object that provides access to the uploaded file info/content. At his point the file contents are already stored in memory or in a temp folder unless the MultipartResolver implementation chooses to do so lazily.

At the end of request processing in DispatcherServlet.doDispatch() method (in the finally block), MultipartResolver.cleanupMultipart() is called so that underlying implementation can cleanup any resources used for the multipart handling, like a storage for the uploaded files.

MultipartResolver implementations

Spring provides two implementations:

  • (1)CommonsMultipartResolver

    This resolver uses Apache FileUpload API.

  • (2)StandardServletMultipartResolver

    This resolves uses Servlet 3.0 multipart support.

The controller

MultipartHttpServletRequest.getFile('fileName') and other methods can be used in our controller to access the uploaded file info/content:

 @PostMapping("/form")
 public String handleRequest(HttpServletRequest request, ...) {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    MultipartFile multipartFile = multipartRequest.getFile("user-file");
    ...
  }

Using MultipartHttpServletRequest

MultipartFile is an interface which represents the uploaded file received in the multipart request.

We can also use MultipartHttpServletRequest as handler method parameter

public String handlePostRequest (MultipartHttpServletRequest request, ...)  {
   MultipartFile multipartFile = request.getFile("user-file");
    ......
   return "response";
}

Using MultipartFile

We can also use MultipartFile as handler method parameter

 @PostMapping("/form")
  public String handleUpload(@RequestParam("user-file") MultipartFile file, ...) {

        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            // do something with the content
            ...
        }
      ....
    }

Using javax.servlet.http.Part

When using StandardServletMultipartResolver, we can also use javax.servlet.http.Part as a method parameter instead of Spring's MultipartFile.

 @PostMapping("/form")
  public String handleUpload(@RequestParam("user-file") javax.servlet.http.Part part, ...) {
     InputStream inputStream = part.getInputStream();
      ....
    }

In next tutorials, we will see examples on how to use file uploads in Spring.

See Also