title: Spring-Boot + Tomcat 8.0.3 FileUpload error Unable to process parts as no multi-part configuration has been provided
body: <p>I have a working spring-boot mvc application where i am trying integrate a file upload option. As per manual I have configured the following template: </p>
  
  <p>/src/main/resource/templates/fileUpload.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html layout:decorator="layout"&gt;
  
  &lt;head&gt; 
  &lt;title&gt;syncServer File upload example&lt;/title&gt;
  &lt;/head&gt;
    &lt;body&gt;
      &lt;div layout:fragment="content"&gt;
  
      &lt;div &gt;
          &lt;form th:action="@{/upload}" enctype="multipart/form-data" th:method="post"&gt;
              &lt;fieldset&gt;
                       &lt;legend&gt;File to upload:&lt;/legend&gt;
                       &lt;input type="file" name="myFile" /&gt;
                       &lt;label&gt;Name :&lt;/label&gt;
                       &lt;input type="text" name="myName"/&gt;
                       &lt;input type="submit" /&gt;
              &lt;/fieldset&gt;
      &lt;/form&gt;
      &lt;/div&gt;
  
      &lt;/div&gt;
  
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>when i login and then call http:/127.0.0.5:8080/upload the form displays but when i click the submit button i got. the error message</p>
  
  <blockquote>
    <p>HTTP Status 403 - Invalid CSRF Token 'null' was found on the request
    parameter '_csrf' or header 'X-CSRF-TOKEN'.</p>
  </blockquote>
  
  <p>The i went and disable csrf in order to see if the form will simply work if the cross site request forgery is disabled (".csrf().disable()"). 
  i.e. </p>
  
  <pre><code>   @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .csrf().disable()
  //          .headers().disable()
              .authorizeRequests()
                  .antMatchers("/resources/css/**","/register", "/resources/img/**" , "/resources/js/**", "/resources/pdf/**").permitAll()
                  .anyRequest().authenticated()
                  .and()
              .formLogin()
                  .loginPage("/login")
                  .permitAll()
                  .and()
              .logout()
                  .permitAll();
      }
  </code></pre>
  
  <p>When i tried to upload a another file during the csrf was disabled i got another error i.e.:</p>
  
  <blockquote>
    <p>HTTP Status 500 - Request processing failed; nested exception is
    org.springframework.web.multipart.MultipartException: Could not parse
    multipart servlet request; nested exception is
    java.lang.IllegalStateException: Unable to process parts as no
    multi-part configuration has been provided</p>
  </blockquote>
  
  <p>it says "no multi-part configuration has been provided" but i do have configuration file that configures the multipartConfigElement i.e.</p>
  
  <pre><code>@Configuration
  public class FileUpload {
      @Bean
      MultipartConfigElement multipartConfigElement() {
          MultiPartConfigFactory factory = new MultiPartConfigFactory();
          factory.setMaxFileSize("600KB");
          factory.setMaxRequestSize("600KB");
          return factory.createMultipartConfig();
      }
  }
  </code></pre>
  
  <p>I have also the @ComponentScan anotation and the @EnableAutoConfiguration above my Application class i.e.:</p>
  
  <pre><code>@ComponentScan(basePackages = "org.syncServer.*")
  @Configuration
  @EnableWebSocketMessageBroker
  @EnableAutoConfiguration
  public class Application extends SpringBootServletInitializer {
  ...
  }
  </code></pre>
  
  <p>When the application starts i am listing all the beans that are managed by spring and i do see that the bean "multipartResolver" is being initiated.</p>
  
  <p>Controller associated with the fileUpload.html: </p>
  
  <pre><code>@Controller
  public class FileUploadController {
  
  
      @RequestMapping(value="/upload", method=RequestMethod.GET)
      public String provideUploadInfo() {
          return "fileUpload";
      }
  
  
      @RequestMapping(value="/upload", method=RequestMethod.POST)
      public @ResponseBody String handleFileUpload(@RequestParam("myName") String myName, 
              @RequestParam("myFile") MultipartFile myFile){
          if (!myFile.isEmpty()) {
              try {
                  byte[] bytes = myFile.getBytes();
                  BufferedOutputStream stream = 
                          new BufferedOutputStream(new FileOutputStream(new File(myName + "-uploaded")));
                  stream.write(bytes);
                  stream.close();
                  return "Successfully uploaded " + myName + " into " + myName + "-uploaded !";
              } catch (Exception e) {
                  return "Failed to upload " + myName + " =&gt; " + e.getMessage();
              }
          } else {
              return "Failed to upload " + myName + " empty file.";
          }
      }
  
  }
  </code></pre>
  
  <p>When i set a breakpoint at the get method i see that the method is hit and it returns the fileUpload.html view. However the post method is not hit at all.  </p>
  
  <p>I do not know if that is relevant but i have set in Project-Properties-Deployment assembly - /src/main/resources as "/" this is the only entry i have in my Deployment Assembly configuration.</p>
  
  <p>I have read before that there was a bug in old spring-boot RC version with the file uploads. I am currently using the spring-boot 1.0.2.RELEASE. 
  My pom file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.0.2.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
  
      &lt;groupId&gt;sync&lt;/groupId&gt;
      &lt;artifactId&gt;syncServer&lt;/artifactId&gt;
      &lt;name&gt;syncServer&lt;/name&gt;
  
      &lt;!-- When removing the parent project configuration you have to explicitly set the dependencies version --&gt;
      &lt;dependencies&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;${hibernate}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;postgresql&lt;/groupId&gt;
              &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
              &lt;version&gt;9.1-901.jdbc4&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.validation&lt;/groupId&gt;
              &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
              &lt;version&gt;1.1.0.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;
              &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-messaging&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.thymeleaf&lt;/groupId&gt;
              &lt;artifactId&gt;thymeleaf-spring4&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-websocket&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
              &lt;artifactId&gt;tomcat-embed-core&lt;/artifactId&gt;
              &lt;version&gt;${tomcat8.version}&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
              &lt;artifactId&gt;tomcat-embed-websocket&lt;/artifactId&gt;
              &lt;version&gt;8.0.3&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
  
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-websocket&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
              &lt;version&gt;3.2.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
              &lt;version&gt;3.2.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-core&lt;/artifactId&gt;
              &lt;version&gt;3.2.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.thymeleaf&lt;/groupId&gt;
              &lt;artifactId&gt;thymeleaf&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.thymeleaf.extras&lt;/groupId&gt;
              &lt;artifactId&gt;thymeleaf-extras-springsecurity3&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;nz.net.ultraq.thymeleaf&lt;/groupId&gt;
              &lt;artifactId&gt;thymeleaf-layout-dialect&lt;/artifactId&gt;
              &lt;version&gt;1.2.1&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;build&gt;
          &lt;defaultGoal&gt;test&lt;/defaultGoal&gt;
          &lt;plugins&gt;
  
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
  
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  
                  &lt;configuration&gt;
                      &lt;source&gt;1.8&lt;/source&gt;
                      &lt;target&gt;1.8&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;useSystemClassLoader&gt;false&lt;/useSystemClassLoader&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
  
  
          &lt;/plugins&gt;
  
  
  
      &lt;/build&gt;
  
  
  
    &lt;repositories&gt;
      &lt;repository&gt;
          &lt;id&gt;spring-milestones&lt;/id&gt;
          &lt;name&gt;Spring Milestones&lt;/name&gt;
          &lt;url&gt;http://repo.spring.io/milestone&lt;/url&gt;
          &lt;snapshots&gt;
              &lt;enabled&gt;false&lt;/enabled&gt;
          &lt;/snapshots&gt;
      &lt;/repository&gt;
  &lt;/repositories&gt;
  
      &lt;pluginRepositories&gt;
          &lt;pluginRepository&gt;
              &lt;id&gt;spring-snapshots&lt;/id&gt;
              &lt;url&gt;http://repo.spring.io/libs-snapshot&lt;/url&gt;
              &lt;snapshots&gt;&lt;enabled&gt;true&lt;/enabled&gt;&lt;/snapshots&gt;
          &lt;/pluginRepository&gt;
  
          &lt;pluginRepository&gt;
              &lt;id&gt;spring-milestones&lt;/id&gt;
              &lt;url&gt;http://repo.spring.io/milestone&lt;/url&gt;
          &lt;/pluginRepository&gt;
  
  
      &lt;/pluginRepositories&gt;
  
      &lt;properties&gt;
          &lt;java-version&gt;1.7&lt;/java-version&gt;
          &lt;spring-version&gt;4.0.2.RELEASE&lt;/spring-version&gt;
          &lt;org.aspectj-version&gt;1.6.10&lt;/org.aspectj-version&gt;
          &lt;org.slf4j-version&gt;1.7.6&lt;/org.slf4j-version&gt;
          &lt;start-class&gt;org.syncServer.core.Application&lt;/start-class&gt;
          &lt;springBootVersion&gt;1.0.2.RELEASE&lt;/springBootVersion&gt;
          &lt;tomcat8.version&gt;8.0.3&lt;/tomcat8.version&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;hibernate&gt;4.3.1.Final&lt;/hibernate&gt;
      &lt;/properties&gt;
  
  
  
  &lt;/project&gt;
  </code></pre>
  
  <p>Addition here is the full html code as requested in the comments:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  
  &lt;html&gt;
    &lt;head&gt;
  &lt;title&gt;Task List - syncServer File upload example&lt;/title&gt;
  
      &lt;link rel="stylesheet" type="text/css" media="all" href="/css/syncServer.css" /&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
      &lt;script src="/js/jquery.js"&gt;&lt;/script&gt;
  
  
  
  &lt;/head&gt;
  
    &lt;body&gt;
       &lt;div class="navbar navbar-inverse navbar-fixed-top"&gt;
        &lt;div class="container"&gt;
          &lt;div class="navbar-header"&gt;
            &lt;button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse"&gt;
              &lt;span class="icon-bar"&gt;&lt;/span&gt;
              &lt;span class="icon-bar"&gt;&lt;/span&gt;
              &lt;span class="icon-bar"&gt;&lt;/span&gt;
            &lt;/button&gt;
            &lt;a class="navbar-brand" href="#"&gt;My project&lt;/a&gt;
          &lt;/div&gt;
          &lt;div class="navbar-collapse collapse"&gt;
            &lt;ul class="nav navbar-nav"&gt;
              &lt;li class="active"&gt;&lt;a href="/"&gt;Home&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="/message"&gt;OpenSource&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="/task"&gt;Documents&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
            &lt;ul class="nav navbar-nav navbar-right"&gt;
  &lt;!--             &lt;li th:if="${#authorization.expression('!isAuthenticated()')}"&gt; --&gt;
  &lt;!--               &lt;a href="/signin" th:href="@{/signin}"&gt;Sign in&lt;/a&gt; --&gt;
  &lt;!--             &lt;/li&gt; --&gt;
  &lt;!--             &lt;li th:if="${#authorization.expression('isAuthenticated()')}"&gt; --&gt;
  &lt;!--               &lt;a href="/logout" th:href="@{/logout}"&gt;Logout&lt;/a&gt; --&gt;
  &lt;!--             &lt;/li&gt; --&gt;
            &lt;/ul&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="container"&gt;
        &lt;div&gt;
  
      &lt;div&gt;
          &lt;form enctype="multipart/form-data" method="post" action="/upload"&gt;
              &lt;fieldset&gt;
                       &lt;legend&gt;File to upload:&lt;/legend&gt;
                       &lt;input type="file" name="myFile" /&gt;
                       &lt;label&gt;Name :&lt;/label&gt;
                       &lt;input type="text" name="myName" /&gt;
                       &lt;input type="submit" /&gt;
              &lt;/fieldset&gt;
      &lt;/form&gt;
      &lt;/div&gt;
  
      &lt;/div&gt;
        &lt;div&gt;
        © 2014
        &lt;br /&gt;
        Autumn, Winter, Summer , Spring is always inside.
      &lt;/div&gt;
      &lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Addition 2 link to the working maven project showing the problem</p>
  
  <pre><code>https://github.com/TheDictator/sArchitecture
  </code></pre>
  
  <p>in order to reproduce login with:</p>
  
  <p>user name: admin2 password: #passworD</p>
  
  <p>and go to </p>
  
  <pre><code>http://127.0.0.5:8080/upload
  </code></pre>
  
  <p>the error message described above is see.</p>
  
  <p>Addition 3 </p>
  
  <p>My particular problem is fixed when i removed the bean that i have previously declared before the entry point mail in the application class i.e. </p>
  
  <pre><code>@Bean
  public ServletRegistrationBean dispatcherRegistration() {
  
      System.out.println("SERVLET REGISTRATION");
      ServletRegistrationBean registration = new ServletRegistrationBean(
              dispatcherServlet());
  
      System.out.println("SERVLET REGISTERED NAME is: "
              + registration.getServletName().toString());
      registration.addUrlMappings("/");
  
      return registration;
  }
  </code></pre>
  
  <p>I have not answered this question my self as i do not yet understand why this peace of code is messing up the upload functionality. </p>