title: Spring rest MultiPart file upload with Java configuration without Spring boot
body: <p>I am trying to implement a rest web service that uses <code>MultipartFile</code> to upload a file using Spring, with java configuration. I do not use Spring Boot and I have <code>commons-fileupload</code> library in my classpath.</p>
  
  <p>I read Spring documentation that says: </p>
  
  <blockquote>
    <p>you need to mark the DispatcherServlet with a "multipart-config" section in web.xml, or with a javax.servlet.MultipartConfigElement in programmatic Servlet registration, or in case of a custom Servlet class possibly with a javax.servlet.annotation.MultipartConfig annotation on your Servlet class ... Once Servlet 3.0 multipart parsing has been enabled in one of the above mentioned ways you can add the StandardServletMultipartResolver to your Spring configuration</p>
  </blockquote>
  
  <p>Hence I added this bean to my <code>AppConfig</code> class:</p>
  
  <pre><code> @Bean
   public StandardServletMultipartResolver multipartResolver() {
      return new StandardServletMultipartResolver();
   }
  </code></pre>
  
  <p>and annotated the class with <code>MultipartConfig</code>:</p>
  
  <pre><code>@EnableWebMvc
  @MultipartConfig(maxFileSize = 5120)
  public class AppConfig extends WebMvcConfigurerAdapter{
   ...
  }
  </code></pre>
  
  <p>but I get this exception when I call the service:</p>
  
  <pre><code>Caused by: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.UnsupportedOperationException: SRVE8020E: Servlet does not accept multipart request
      at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:111)
      at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.&lt;init&gt;(StandardMultipartHttpServletRequest.java:85)
      at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:76)
      at org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:112)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:207)
      at [internal classes]
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:207)
      ... 1 more
  Caused by: java.lang.UnsupportedOperationException: SRVE8020E: Servlet does not accept multipart request
      at com.ibm.ws.webcontainer.srt.SRTServletRequest.prepareMultipart(SRTServletRequest.java:3657)
      at [internal classes]
      at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:92)
  </code></pre>
  
  <p>If I use <code>CommonsMultipartResolver</code> instead of <code>StandardServletMultipartResolver</code> I get the same error.</p>
  
  <p>This is how I initialize my application:</p>
  
  <pre><code>public class AppInitializer implements WebApplicationInitializer {
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
          context.register(AppConfig.class);
          context.setServletContext(servletContext);
  
          servletContext.addListener(new ContextLoaderListener(context));
  
          ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcherServlet", new DispatcherServlet(context));
          dispatcher.setLoadOnStartup(1);
          dispatcher.addMapping("/");
  
  
          CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
          characterEncodingFilter.setEncoding("UTF-8");
          characterEncodingFilter.setForceEncoding(true);
  
          EnumSet&lt;DispatcherType&gt; dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
          FilterRegistration.Dynamic characterEncoding = servletContext.addFilter("CharacterEncodingFilter", characterEncodingFilter);
          characterEncoding.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
  
      }
  }
  </code></pre>
  
  <p>I also tried add a <code>MultipartFilter</code> but with no luck.</p>
  
  <pre><code>MultipartFilter multipartFilter = new MultipartFilter();
  FilterRegistration.Dynamic multipart = servletContext.addFilter("multipartFilter", multipartFilter);
      multipart.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
  </code></pre>
  
  <p>Is this necessary? What am I doing wrong? I think I read the whole internet searching for a solution but they all use spring boot with <code>MultipartConfigElement</code> and <code>MultipartConfigFactory</code>. Maybe the problem is the way I consume the service?</p>
  
  <p>This is my controller method:</p>
  
  <pre><code>@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = "multipart/form-data" )
  public Long uploadAttachment(@RequestParam("cn") String callerName, @RequestParam("cs") String callerService, @RequestParam("file")  MultipartFile file)
  </code></pre>
  
  <p>and this is how i consume it:</p>
  
  <pre><code>File file = new File("C:\\Users\\cte0289\\Documents\\Email\\document.docx");
  RestTemplate rest = new RestTemplate();
  LinkedMultiValueMap&lt;String, Object&gt; map = new LinkedMultiValueMap&lt;String, Object&gt;();
  
  map.add("cn", callerName);
  map.add("cs", callerService);
  map.add("file", file);            
  Long response = rest.postForObject(url + "/upload", map, Long.class);
  </code></pre>
  
  <p>Please help I don't know what else to do.</p>