title: Making a multipart post request with compressed jpeg byte array with spring for android
body: <p>I've been using spring for android successfully in my android app to get/post data from/to the server. Now, I have to do a post request for a <strong>multipart form</strong>, but I've been unable to get it working the way I want.</p>
  
  <p>Use case:
  1. Pick a photo from the gallery
  2. Load it to a bitmap using the file source
  3. Compress the bitmap to a ByteArrayOutputStream
  4. Pass the byte array ( ByteArrayOutputStream.toByteArray() ) to the server. (I need to send this as jpeg not application/octet-stream)</p>
  
  <p>The server endpoint for upload photo accepts a MultipartFile with only the following Mime types ( Note, it <strong>does not accept MimeType: application/octet-stream</strong> ):</p>
  
  <pre><code>GIF("image/gif")
  PNG("image/png", "image/x-png")
  JPG("image/jpeg", "image/jpg", "image/pjpeg")
  BMP("image/bmp")
  </code></pre>
  
  <p>I've tried using the <a href="https://github.com/SpringSource/spring-android-samples/blob/master/spring-android-showcase/client/src/org/springframework/android/showcase/rest/HttpPostFormDataActivity.java" rel="noreferrer">sample code</a>, but been unsuccessful so far.</p>
  
  <p>With the following code I get the following error:
  org.springframework.web.bind.MissingServletRequest ParameterException: Required MultipartFile parameter 'file' is not present</p>
  
  <p>Help on this matter is greatly appreciated. Thanks and keep up the good work.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>bitmap = BitmapFactory.decodeFile("/mnt/sdcard/DCIM/Camera/20130205_162546.jpg");
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  bitmap.compress(CompressFormat.JPEG, 60, bos);
  byte[] data = bos.toByteArray();
  
  // populate the data to post
  MultiValueMap&lt;String, Object&gt; formData = new LinkedMultiValueMap&lt;String, Object&gt;();
  formData.add("caption", "Test Caption");
  formData.add("file", data);
  
  // The URL for making the POST request
  final String url = "http://api.example.com/imageUpload?oauth_token=XXXXXX";
  
  HttpHeaders requestHeaders = new HttpHeaders();
  
  // Sending multipart/form-data
  requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
  
  // Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
  HttpEntity&lt;MultiValueMap&lt;String, Object&gt;&gt; requestEntity = new HttpEntity&lt;MultiValueMap&lt;String, Object&gt;&gt;(formData, requestHeaders);
  
  // Create a new RestTemplate instance
  RestTemplate restTemplate = new RestTemplate(true);
  
  //    // Set a custom message converter that supports the application/json media type
  //    final GsonHttpMessageConverter gsonMessageConverter = new GsonHttpMessageConverter();
  //    gsonMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
  //    final ByteArrayHttpMessageConverter byteArrayMessageConverter = new ByteArrayHttpMessageConverter();
  //    byteArrayMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.IMAGE_JPEG));
  //    final FormHttpMessageConverter formMessageConverter = new FormHttpMessageConverter();
  //    formMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.IMAGE_JPEG));
  //    restTemplate.getMessageConverters().addAll(Arrays.asList(gsonMessageConverter, byteArrayMessageConverter, formMessageConverter));
  
  // Make the network request, posting the message, expecting a String in response from the server
  ResponseEntity&lt;String&gt; response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
  
  // Return the response body to display to the user
  Log.i(TAG, "**** response.body : " + response.getBody());
  </code></pre>