title: Convert BufferedInputStream into image
body: <p>Im having trouble turning my blob into a buffered image so I can use it. I'm getting a blob (jpg image) back from my database I uploaded using inputstream. In my database it is stored as BufferedInputStream I notice. I get the blob just fine, its a bunch of weird symbols and says its a jpg so the image has to be fine. Can anyone spot what I'm doing wrong? Maybe im converting it wrong? At image = ImageIO.read(new ByteArrayInputStream(data)); Image returns null.</p>
  
  <pre><code>@GET
  @Path("{id}")
   @Produces("image/*")
  public Response post(@PathParam("id") String id) throws IOException {
      Connection con = connection();
      Blob blob = getPhoto(con);
      BufferedImage image = null;
      byte[] data = null;
      int blobLength = 0;
      try {
          blobLength = (int) blob.length();
          data = blob.getBytes(1, blobLength);
          image = ImageIO.read(new ByteArrayInputStream(data));
      //  ImageIO.write(image, "JPEG", new File("C:/Users/Nicolas/Desktop/image.jpg"));
      } catch (SQLException e2) {
          e2.printStackTrace();
      }  
  
      return Response.ok(image).build();
  }
  </code></pre>
  
  <p>How I write to database</p>
  
  <pre><code>public void postPhoto(Connection con, InputStream uploadedInputStream){
  
  String mySQL = "INSERT INTO photos (photo) values (?)";
  PreparedStatement pStmt = con.prepareStatement(mySQL);
  pStmt.setBlob(1, uploadedInputStream);
  pStmt.execute();
  }
  </code></pre>
  
  <p>How I send the file to my servlet</p>
  
  <pre><code>var fileInput = document.getElementById('file');
  var file = fileInput.files[0];
  var formData = new FormData();
  formData.append("file", file);
  
  var parameters="first="+firstName+"&amp;last="+lastName+"&amp;file="+file;
  
  xmlhttp.open("post","http://localhost:8080/restService/api/submitinfo",true);
  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlhttp.send(formData);
  </code></pre>