title: Spring MVC: HTTP 400 Error when trying to upload csv files
body: <p><strong>Problem</strong>: I am following this guide to upload files via Spring MVC: <a href="https://spring.io/guides/gs/uploading-files/" rel="nofollow noreferrer">https://spring.io/guides/gs/uploading-files/</a> However when I do so, I keep getting the following error when I upload a file via the form on my .jsp page:</p>
  
  <blockquote>
    <p>HTTP Status 400 - Required String parameter 'name' is not present</p>
    
    <p>type Status report</p>
    
    <p>message Required String parameter 'name' is not present</p>
    
    <p>description The request sent by the client was syntactically
    incorrect.</p>
    
    <p>Apache Tomcat/7.0.52</p>
  </blockquote>
  
  <p><strong>Question</strong>: How do I resolve the 400 error and upload the file successfully?</p>
  
  <p><strong>Approach</strong>: Through following the guide, I have created a controller class, TestController, and a method called handleFileUpload, which is what deals with the user uploading a file. I stayed away from using the multipartResolver bean as it gave me all sorts of errors related to Apache Commons found in a previous question I asked here: <a href="https://stackoverflow.com/questions/35467250/unable-to-upload-a-file-using-spring-apache-commons-issue">https://stackoverflow.com/questions/35467250/unable-to-upload-a-file-using-spring-apache-commons-issue</a></p>
  
  <p><strong>Code</strong></p>
  
  <p>TestController.java</p>
  
  <pre><code>    @Controller
      public class TestController {
  
         @RequestMapping(value="/upload", method=RequestMethod.POST)
         public @ResponseBody String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) {
          if (!file.isEmpty()) {
              try {
                  byte[] bytes = file.getBytes();
                  BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
                  stream.write(bytes);
                  stream.close();
                  return "You successfully uploaded " + name + "!";
              } catch (Exception e) {
                  return "You failed to upload " + name + " =&gt; " + e.getMessage();
              }
          } else {
              return "You failed to upload " + name + " because the file was empty.";
          }
        }
  
  
      }
  </code></pre>
  
  <p>Here is the form within index.jsp. Keep in mind I am using TWBS within my jsp pages, I am not sure if that would make any huge difference in this.</p>
  
  <pre><code>&lt;form method="POST" enctype="multipart/form-data" action="/upload"&gt;
      File to upload: &lt;input type="file" name="file"&gt;&lt;br/&gt;
      Name: &lt;input type="text" name="name"&gt;&lt;br/&gt;&lt;br/&gt;
      &lt;input type = "submit" value = "Upload"&gt; Press here to upload the file!
  &lt;/form&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Update 2.22.16</strong>
  Orest's comment was helpful, however when I do follow link (<a href="http://www.concretepage.com/spring-4/spring-4-mvc-single-multiple-file-upload-example-with-tomcat" rel="nofollow noreferrer">http://www.concretepage.com/spring-4/spring-4-mvc-single-multiple-file-upload-example-with-tomcat</a>) I get the following error:</p>
  
  <pre><code>HTTP Status 500 - Servlet.init() for servlet mvc-dispatcher threw exception
  
  type Exception report
  
  message Servlet.init() for servlet mvc-dispatcher threw exception
  
  description The server encountered an internal error that prevented it from fulfilling this request.
  
  exception
  
  javax.servlet.ServletException: Servlet.init() for servlet mvc-dispatcher threw exception
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      java.lang.Thread.run(Thread.java:745)
  root cause
  
  java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
      java.lang.Class.getDeclaredMethods0(Native Method)
      java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
      java.lang.Class.getDeclaredMethods(Class.java:1975)
      org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:609)
      org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:521)
      org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:507)
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1069)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
      org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:838)
      org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
      org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
      org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
      org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
      org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
      org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
      org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      javax.servlet.GenericServlet.init(GenericServlet.java:158)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      java.lang.Thread.run(Thread.java:745)
  root cause
  
  java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
      org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
      org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
      java.lang.Class.getDeclaredMethods0(Native Method)
      java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
      java.lang.Class.getDeclaredMethods(Class.java:1975)
      org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:609)
      org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:521)
      org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:507)
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1069)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
      org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:838)
      org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
      org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
      org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
      org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
      org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
      org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
      org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      javax.servlet.GenericServlet.init(GenericServlet.java:158)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      java.lang.Thread.run(Thread.java:745)
  note The full stack trace of the root cause is available in the Apache Tomcat/7.0.52 logs.
  
  Apache Tomcat/7.0.52
  </code></pre>
  
  <p>A note to add as to why I receive the above error: whenever I add the following bean to my <code>mvc-dispatcher-servlet.xml</code> file, the above error is given when I try to upload a file: <code>&lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /&gt;</code></p>
  
  <hr>
  
  <p><strong>Update #2 2.22.16</strong>
  oshi would like to know if the correct jar is in my classpath. I am using IntelliJ and it is in my External Libraries folder. Please view the screenshot below:
  <a href="https://i.stack.imgur.com/7tnvg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7tnvg.png" alt="enter image description here"></a></p>