title: Upload image as byte[] via Spring Rest
body: <p>I am trying to upload an image file as a <code>byte[]</code> object. At the web service, convert this <code>byte[]</code> into <code>BufferedImage</code> and perform some processing before returning the <code>byte[]</code> as a response to the client. </p>
  
  <p>But I am always getting null pointer when <code>ImageIO.read(byte[] input)</code> is called. I noticed that the <code>byte[]</code> length before and after invoking the service is different. Please help. Following the codes for the client and the rest web service.</p>
  
  <p>client code:
      CloseableHttpClient httpclient = null;</p>
  
  <pre><code>    final String uri = "http://localhost:8080/core/uploadForMarkImg";
  
      File source_imgFile = new File("C:\\watermarktest\\source.jpg");
  
  
      try
      {
      ImageInputStream iis = ImageIO.createImageInputStream(source_imgFile);
  
      Iterator&lt;ImageReader&gt; imageReaders = ImageIO.getImageReaders(iis);
  
      while (imageReaders.hasNext()) {
          ImageReader reader = (ImageReader) imageReaders.next();
          System.out.printf("formatName: %s%n", reader.getFormatName());
      }
      }catch (Exception ex)
      {
          ex.printStackTrace();
      }
  
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try
      {
  
      BufferedImage img=ImageIO.read(source_imgFile);
      ImageIO.write(img, "jpg", bos);
      bos.flush();
      }
      catch(Exception ex)
      {
          ex.printStackTrace();
      }
  
  httpclient = HttpClients.custom().build();
      HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addBinaryBody(
              "imgFile", bos.toByteArray(), ContentType.APPLICATION_OCTET_STREAM, source_imgFile.getName()).build();
  
      post.setEntity(entity);
  </code></pre>
  
  <p>Server Code:</p>
  
  <pre><code>@RequestMapping(value = "/uploadForMarkImg", method = RequestMethod.POST, consumes = { "application/octet-stream" })
  public ResponseEntity&lt;byte[]&gt; imgMark(@RequestBody byte[] imgFile) {
  
  
      try {
          ClassLoader classLoader = getClass().getClassLoader();
          File watermarkImageFile = new File(classLoader.getResource("watermark.png").getFile());
  
  
          System.out.println("input file length " + imgFile.length);
          BufferedImage resizeOriginal = null;
          InputStream inputStream = new ByteArrayInputStream(imgFile);
          BufferedImage sourceImage=ImageIO.read(new ByteArrayInputStream(imgFile));
  System.out.println("converted to buffer image completed for source image " + sourceImage);
  
          BufferedImage watermarkImage = ImageIO.read(watermarkImageFile);
  
          System.out.println("converted to buffer image completed for watermark image " + watermarkImageFile);
  
          System.out.println("SouceImage Size Width: " + sourceImage.getWidth());
          System.out.println("SouceImage Size Height: " + sourceImage.getHeight());
  </code></pre>
  
  <p>sourceImage.get will always get nullpointer errror.</p>