title: File Upload using Spring MVC
body: <p>I am new to Spring MVC though not new to Java and working primary on Struts2 and Wicket as my choice for web development</p>
  
  <p>I am trying to do a POC of file upload using spring MVC here is my jsp file</p>
  
  <pre><code>&lt;form  id="fileuploadForm" action="fileupload" method="POST" enctype="multipart/form-data" &gt;
              &lt;fieldset&gt;
                  &lt;legend&gt;Upload Fields&lt;/legend&gt;
  
                 &lt;input id="file" type="file" name="file" /&gt;
                  &lt;p&gt;&lt;button type="submit"&gt;Upload&lt;/button&gt;&lt;/p&gt; 
  
              &lt;/fieldset&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p>and my Controller is</p>
  
  <pre><code>@Controller
  @RequestMapping("FileUpload/fileupload")
  public class FileUploadController{
  
  
  
      public ModelAndView processUpload(@RequestParam MultipartFile file, WebRequest webRequest, Model model) {
  
  
          String orgFileName = file.getOriginalFilename();
          String filePath = "data/input" + orgFileName;
          ModelMap modelMap = new ModelMap(); 
          System.out.println("*******************************************");
          File dest = new File(filePath);
          try {
              file.transferTo(dest);
          } catch (IllegalStateException e) {
              e.printStackTrace();
              modelMap.addAttribute("result", "File uploaded failed:" + orgFileName);
              return new ModelAndView("results", modelMap);
                  //return "File uploaded failed:" + orgFileName;
          } catch (IOException e) {
              e.printStackTrace();
              modelMap.addAttribute("result", "File uploaded failed:" + orgFileName);
              return new ModelAndView("results", modelMap);
          }
  
  
          modelMap.addAttribute("result", "File uploaded " + orgFileName);
          return new ModelAndView("results", modelMap);
  
  
  
      }
  </code></pre>
  
  <p>below is the entry for dispatcher-servlet.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"&gt;
  
       &lt;bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /&gt;
       &lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/&gt;
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
           p:suffix=".jsp" /&gt;
  
      &lt;bean name="/*" class="com.app.controller.FileUploadController"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>i tried google also but not able to get any help may be not able to find good resource due to lack of knowledge of spring MVC
  where ever i am hitting my uplaod button i am getting 404 error for this URL</p>
  
  <pre><code>http://localhost:7777/FileUpload/fileupload
  </code></pre>
  
  <p>i am sure i am doing some configuration mistake but not able to point it out, any help in this will be very helpful</p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      id="WebApp_ID" version="2.5"&gt;
      &lt;display-name&gt;SpringExample17&lt;/display-name&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;WEB-INF/dispatcher-servlet.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;
          org.springframework.web.context.ContextLoaderListener
          &lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.htm&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;    
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Thanks in advance</p>