Spring MVC file upload – Unable to process parts as no multi-part configuration has been provided

When you implement file upload in a spring mvc application, sometimes you may encounter an error message like “Unable to process parts as no multi-part configuration has been provided“. This is because your servlet is version 3.0 or newer and tomcat need you to set multipart configure settings to the servlet instance. This article will tell you how to do.

1. Set Multipart Config In Code.

If your spring project use JavaConfig ( not using xml configuration ), then you can set multipart config to the dispatcher servlet in the configuration servlet onStartup method like below.

import javax.servlet.FilterRegistration;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.multipart.support.MultipartFilter;
import org.springframework.web.servlet.DispatcherServlet;

@Configuration
public class ExampleDispatcherServlet implements WebApplicationInitializer {

      public void onStartup(ServletContext servletContext) throws ServletException {

            AnnotationConfigWebApplicationContext dispatcherServletContext = new AnnotationConfigWebApplicationContext();

            dispatcherServletContext.register(ExampleDispatcherConfig.class);

            DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherServletContext);

            ServletRegistration.Dynamic dispatcher = servletContext.addServlet("simple-example", dispatcherServlet);

            /* If do not set multipartcofgin in servlet 3, when you upload a file, it will throw Unable to process parts as no multi-part configuration has been provided error. */
            MultipartConfigElement multipartConfig = new MultipartConfigElement("/tmp");
            dispatcher.setMultipartConfig(multipartConfig);

            dispatcher.setLoadOnStartup(1);

            dispatcher.addMapping("*.html");

            FilterRegistration.Dynamic multipartFilter = servletContext.addFilter("multipartFilter", MultipartFilter.class);
            multipartFilter.addMappingForUrlPatterns(null, true, "/*");

     }

}

2. Set Multipart Config In Web.xml.

If you use xml configuration for your spring project, you should add below xml code in WEB-INF/web.xml file.

<multipart-config>
    <location>/tmp</location>
    <max-request-size>418018841</max-request-size>
    <max-file-size>20848820</max-file-size>
    <file-size-threshold>1048576</file-size-threshold>
</multipart-config>

3. Use MultipartConfig Annotation.

@javax.servlet.annotation.MultipartConfig
public class Dispatcher extends HttpServlet
{

}

Conclusion

  1. If your html form contains enctype=’multipart/form-data‘, then your servlet which process file upload must be configured to support multipart configuration.
READ :   Spring Framework Overview

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.