title: Uploading a file using Spring MVC and Thymeleaf
body: <p>I am trying to upload a file to the server using a spring controller. </p>
  
  <p>This is my web config:</p>
  
  <pre><code>   @Bean
     public CommonsMultipartResolver multipartConfigElement() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setDefaultEncoding("utf-8");
        commonsMultipartResolver.setMaxUploadSizePerFile(400000002);
        return commonsMultipartResolver;
     }
  
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(securityInterceptor()).addPathPatterns("/**");
     }
  
     @Bean
     public TemplateResolver templateResolver() {
        TemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        return resolver;
     }
  
     @Bean
     @Autowired
     public SpringTemplateEngine templateEngine(TemplateResolver resolver) {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(resolver);
        return engine;
     }
  
     @Bean
     @Autowired
     public ViewResolver viewResolver(SpringTemplateEngine engine) {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(engine);
        return resolver;
     }
  </code></pre>
  
  <p>And this is my controller:</p>
  
  <pre><code>@RequestMapping(value = "/upload", method = RequestMethod.GET)
     public String uploadFile(HttpServletRequest request)
           throws IOException, ServletException {
        System.out.println("Request: " + request); // REM: Debug Print
  
        final String path = request.getParameter("destination");
        final Part filePart = request.getPart("file");
  
        System.out.println("Path: " + path); // REM: Debug Print
        System.out.println("File Part toString: " + filePart); // REM: Debug Print
        if (filePart != null) {
           System.out.println("File Part name: " + filePart.getName()); // REM: Debug Print
           System.out.println("File Part size: " + filePart.getSize()); // REM: Debug Print
        }
        else {
           System.out.println("Parts is null"); // REM: Debug Print
        }
        return "file_upload_testing";
     }
  </code></pre>
  
  <p>The class is annotated with <code>@MultipartConfig</code>.</p>
  
  <p>I am using Thymeleaf on the front end and I have created a simple form to try get the upload working as follows:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:th="http://www.thymeleaf.org"
        lang="en"&gt;
      &lt;head&gt;
          &lt;title&gt;File Upload&lt;/title&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form method="POST" action="upload" enctype="multipart/form-data" &gt;
              &lt;input type="file" name="file" id="file" /&gt; &lt;br/&gt;
              &lt;input type="text" value="/tmp" name="destination"/&gt;&lt;br/&gt;
              &lt;input type="submit" value="Upload" name="upload" id="upload" /&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>All my other controllers are working, I just cannot get the <code>MultipartFile</code> into the controller.</p>
  
  <p>I have tried using the <code>MultipartFile</code> as a parameter into the my controller and I have also tried casting the <code>HttpServletRequest</code> to a Multipart request. None of this made any difference. </p>
  
  <p>When I make the request to the controller with a text file this is the output:</p>
  
  <pre><code>Request: org.apache.catalina.connector.RequestFacade@48d0006b
  Path: null
  File Part toString(): null
  Parts is null
  </code></pre>
  
  <p>Please help my figure out why the file is not coming through with the request?</p>
  
  <p>[UPDATE] 
  I have tried changing the request mapping to <code>@RequestMapping(value = "/upload", method = RequestMethod.POST)</code> but still get the same output.</p>
  
  <p>I have now also tried:</p>
  
  <p>Controller:</p>
  
  <pre><code>@RequestMapping(value = "/upload", method = RequestMethod.POST)
  public String uploadFile(MultipartFile file)
        throws IOException, ServletException {
     System.out.println("File: " + file); // REM: Debug Print
     return "file_upload_testing";
  }
  </code></pre>
  
  <p>Web config: </p>
  
  <pre><code>   @Bean
     public MultipartResolver mulitpartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setDefaultEncoding("utf-8");
        commonsMultipartResolver.setMaxUploadSizePerFile(400000002);
        return commonsMultipartResolver;
     }
  </code></pre>
  
  <p>With this I get:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
  </code></pre>