Spring MVC - File upload by using StandardServletMultipartResolver

[Last Updated: Feb 1, 2018]

This example shows how to use StandardServletMultipartResolver to upload a file. This resolver is based on the Servlet 3.0 javax.servlet.http.Part API.

Also check out this tutorial to understand how MultipartResolver works.

Other than registering StandardServletMultipartResolver as a bean, we also need to set a javax.servlet.MultipartConfigElement on the Servlet registration which does configuration equivalent to using javax.servlet.annotation.MultipartConfig annotation. Check out this related Servlet example.


We are going to reuse our last example, we just need to modify our Java Config and WebApplicationInitializer implementation.

Java Config

public class MyWebConfig {

  public MultipartResolver multipartResolver() {
      StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
      return resolver;

  public FileUploadController uploadController() {
      return new FileUploadController();
public class MyWebInitializer implements WebApplicationInitializer {

  public void onStartup(ServletContext servletContext) throws ServletException {
      AnnotationConfigWebApplicationContext ctx =
              new AnnotationConfigWebApplicationContext();

      ServletRegistration.Dynamic servlet =
                      new DispatcherServlet(ctx));
      //using MultipartConfigElement(location,maxFileSize,maxRequestSize,fileSizeThreshold)
      MultipartConfigElement multipartConfig = new MultipartConfigElement(
              "c:/temp", 50000000, 50000000, 0);
      //new MultipartConfigElement("c:/temp") will create unlimited sizes and 0 threshold

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war


Output of this example is same as the last example.

Using javax.servlet.http.Part

When using StandardServletMultipartResolver, instead of MultipartFile/MultipartHttpServletRequest as the controller's handler method argument, we can alternatively use javax.servlet.http.Part. Complete example here

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 4.2.4.RELEASE: Spring Web MVC.
  • spring-test 4.2.4.RELEASE: Spring TestContext Framework.
  • javax.servlet-api 3.0.1 Java Servlet API
  • jstl 1.2 javax.servlet:jstl
  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 1.8
  • Maven 3.3.9

Spring File Upload Servlet Resolver Example Select All Download
  • spring-standard-servlet-multipart-resolver
    • src
      • main
        • java
          • com
            • logicbig
              • example
          • webapp
            • WEB-INF
              • views
        • test
          • java
            • com
              • logicbig
                • example

    See Also