title: How to send Multipart form data with restTemplate Spring-mvc
body: <p>I am trying to upload a file with RestTemplate to Raspberry Pi with Jetty. On Pi there is a servlet running:                                                   </p>
  
  <pre><code>protected void doPost(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
  
      PrintWriter outp = resp.getWriter();
  
      StringBuffer buff = new StringBuffer();
  
      File file1 = (File) req.getAttribute("userfile1");
      String p = req.getParameter("path");
      boolean success = false;
  
      if (file1 == null || !file1.exists()) {
          buff.append("File does not exist\n");
      } else if (file1.isDirectory()) {
          buff.append("File is a directory\n");
      } else {
          File outputFile = new File(req.getParameter("userfile1"));
          if(isValidPath(p)){
              p = DRIVE_ROOT + p;
              final File finalDest = new File(p
                      + outputFile.getName());
              success = false;
              try {
                  copyFileUsingFileChannels(file1, finalDest);
                  finalDest.setWritable(true);
                  success = true;
              } catch (Exception e) {
                  e.printStackTrace();
              }
              if (success){
                  buff.append("File successfully uploaded.\n");
              }
              else{
                                      buff.append("Failed to save file.");
              }
          }
          else{
              buff.append("Invalid path.\n");
          }
      }
      outp.write(buff.toString());
  }
  </code></pre>
  
  <p>I am able to successfully do it with curl </p>
  
  <p><code>curl --form userfile1=@/home/pi/src/CreateNewFolderServlet.java --form press=OK localhost:2222/pi/GetFileServlet?path="/media/"</code></p>
  
  <p>This is the method that is supposed to have the same functionality on webapp.</p>
  
  <pre><code>@ResponseBody 
  @RequestMapping(value="/upload/",method=RequestMethod.POST ,produces = "text/plain")
  public String uploadFile(MultipartHttpServletRequest request2, HttpServletResponse response2){
  
      Iterator&lt;String&gt; itr =  request2.getFileNames();
  
       MultipartFile file = request2.getFile(itr.next());
       System.out.println(file.getOriginalFilename() +" uploaded!");
  
      System.out.println(file.toString()); 
       MultiValueMap&lt;String, Object&gt; parts = new LinkedMultiValueMap&lt;String, Object&gt;();
      parts.add("userfile1",file);
      //reqEntity.addPart("userfile1", file);
      String path="/public/";
      RestTemplate restTemplate = new RestTemplate();
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.MULTIPART_FORM_DATA);
      System.out.println("1");
      HttpEntity&lt;MultiValueMap&lt;String, Object&gt;&gt; request = new HttpEntity&lt;MultiValueMap&lt;String, Object&gt;&gt;(parts, headers);
      String url =  url2+"/pi/GetFileServlet?path="+path;
      System.out.println("2");
  /*  restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
      restTemplate.getMessageConverters().add(
              new MappingJackson2HttpMessageConverter());*/
      System.out.println("3");
      ResponseEntity&lt;String&gt; response = restTemplate.exchange(url, HttpMethod.POST, request,String.class);
      System.out.println("4");
      System.out.println("response : " +response);
      if(response==null||response.getBody().trim()==""){
          return "error";
      }
      return response.getBody();
  }
  </code></pre>
  
  <p>This is the output that I get:</p>
  
  <p>ui-elements.html uploaded!</p>
  
  <p>org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@47e7673e</p>
  
  <p>1</p>
  
  <p>2</p>
  
  <p>3                                 </p>
  
  <p>As you can see number 4 is not printed
  No exception in console.
  Exceptions found during debugging:</p>
  
  <pre><code>org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.web.multipart.support.StandardMultipartFile["inputStream"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.web.multipart.support.StandardMultipartFile["inputStream"])
  </code></pre>