title: Upload image to server using POST
body: <p>I know that there are some similar questions to this one on the site, and I've read most of them. I've also Googled for a solution, but am still having issues.</p>
  <p>So, in my project I need to catch multipart post requests that are used to send mixed data (an image, two strings (company, type), date data (month, year) and a few integers) from a client. Upon receiving this request I need to change contrast and brightness of the image, and modify the color to gray tones.</p>
  <p>For debugging I use Postman Chrome Extension, and for now I am only focusing on sending the image. I've tested sending the request in different ways, in half of them I get a &quot;404 Not Found&quot; error, in the other half wrong data is returned (mostly array of 0). Below is the code used in my resource method:</p>
  <pre><code>@Component
  @Path(&quot;/code&quot;)
  @Produces(&quot;application/json&quot;)
  public class EncodeResource extends BasicResource {
      @POST
      @Path(&quot;/encode&quot;)
      @Consumes(&quot;multipart/form-data&quot;)
      public String encodeImage(
              @FormDataParam(&quot;image&quot;) InputStream imageInputStream) {
          BufferedImage imageBuffer = null;
          try {
              imageBuffer = ImageIO.read(imageInputStream);
          } catch (IOException e) {
              e.printStackTrace();
          }
          if (imageBuffer != null)
              return okResponse(&lt;some parameters&gt;);
          else
              return errorResponse(&lt;some parameters&gt;);
      }
  }
  </code></pre>
  <p>ImageInputStream contains some byte array, but ImageIO.read() always returns <em>null</em>. I tried different images with different extensions (which imageio can read by default, checked it), but got the same result. As I think, it happens cause of there are some &quot;unnecessary&quot; information in my inputStream, so imageio.read() can't convert it into any type of image. I've suddenly found it, when changed InputStream to File:</p>
  <pre><code>@FormDataParam(&quot;image&quot;) File imageInputStream
  </code></pre>
  <p>...and then checked saved by tomcat .tmp file. It looked like that:</p>
  <blockquote>
  <p>------WebKitFormBoundary4nC1XD4cevbbRh7A</p>
  <p>Content-Disposition: form-data; name=&quot;image&quot;; filename=&quot;1.jpg&quot;&gt;</p>
  <p>Content-Type: image/jpeg</p>
  <p>(a lot of bytes)</p>
  <p>------WebKitFormBoundary4nC1XD4cevbbRh7A--</p>
  </blockquote>
  <p>And of course imageio.read() returned <em>null</em>.</p>
  <p>Summing up all of that, I have 2 questions actually:</p>
  <ol>
  <li>Why did I get 404 error and how can i fix it? For example, I tried <a href="http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-resteasy/" rel="nofollow noreferrer">this</a> and like <a href="https://stackoverflow.com/questions/15271573/consuming-a-multipart-form-data-via-restful-cxf">here</a>. And I had to delete the second @FormDataParam from <a href="http://puspendu.wordpress.com/2012/08/23/restful-webservice-file-upload-with-jersey/" rel="nofollow noreferrer">here</a> to receive at least something. Can it be cause of the Postman &quot;specific&quot; form-data requests?</li>
  <li>Why did I get the wrong data? Maybe I can parse received file and get &quot;correct&quot; byte array? Or something else?..</li>
  </ol>