title: Spring MVC MultipartFile problems
body: <p>I am trying to run a simple controller with a method that will receive</p>
  
  <ul>
  <li>A String with a value</li>
  <li>A file (MultipartFile object)</li>
  </ul>
  
  <p>After some investigation (<a href="https://stackoverflow.com/questions/4118670/sending-multipart-file-as-post-parameters-with-resttemplate-requests">Sending Multipart File as POST parameters with RestTemplate requests</a>) I ended up adding into my web.xml</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;multipartFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.multipart.support.MultipartFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;multipartFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/REST/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>My application context file</p>
  
  <pre><code>&lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
      &lt;property name="maxUploadSize"&gt;
          &lt;value&gt;104857600&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="maxInMemorySize"&gt;
          &lt;value&gt;4096&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The controller looks like</p>
  
  <pre><code>@Controller
  @RequestMapping("/image")
  public class ImageController  extends RestApiController {
  
  private static final Logger log = LoggerFactory.getLogger(ImageController.class);
  
  @RequestMapping(value="/simple", method = RequestMethod.POST, consumes="multipart/form-data")
  public @ResponseBody boolean save(
          @RequestParam(value = "file", required = false) MultipartFile file,
          @RequestParam(value = "name", required = false) String name) {
  
          //Some code here
      return true;
  }
  </code></pre>
  
  <p>So far I have been able to run unit tests against the controller with no problem, but I seem to be stuck when it comes to create a real http request.</p>
  
  <p>I have tried with POSTMAN, but after some investigation seems that it does not set the multipart/form-data header properly, I have tried removing it, and the problem persists.</p>
  
  <p>I have also tried with CURL</p>
  
  <pre><code>curl http://127.0.0.1:8080/content/REST/image/simple -F "file=@/home/jmoriano/Pictures/simple.jpeg" -F "name=someName" -v 
  </code></pre>
  
  <p>I have also tried with a RestTemplate object</p>
  
  <pre><code>public Boolean update() {
      RestTemplate restTemplate = new RestTemplate();
      try {
          FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
          formConverter.setCharset(Charset.forName("UTF8"));
          restTemplate.getMessageConverters().add(formConverter);
          restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
          HttpHeaders httpHeaders = new HttpHeaders();         httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType("application/json")));
          MultiValueMap&lt;String, Object&gt; parts = new LinkedMultiValueMap&lt;String, Object&gt;();
          parts.add("file", new FileSystemResource("/home/jmoriano/Pictures/simple.jpeg"));
          parts.add("name", "name");
          return restTemplate.postForObject("http://127.0.0.1:8080/content/REST/image/simple", parts, Boolean.class);
      } catch (Exception e) {
          e.printStackTrace();
          log.error("Ouch!", e);
      }
      return false;
  }
  </code></pre>
  
  <p>Just to be clear, the problem is not in the "name" parameter, that one works fine, however the MultipartFile is empty. </p>
  
  <p>By debugging the code, I managed to check the MultiPartFilter class which receives an HttpServletRequest object, such object has its "parts" attribute already empty there. So the problem seems to be related with the way I am making the request... seems that my postman/curl/java attempts have failed... do you see anything incorrect in my config?</p>