title: Java: image <-> bytes conversion return null
body: <p>I'm trying to transfer an image from a client to a server that stocks it into a database. For that, I'm converting my image into a bytes array (it works) and I stock it as blob into the DB (it works).</p>
  
  <pre><code>public static byte[] imageToBytes(String path) {
      byte[] imageByte = null;
      File img = new File(path);
      try {
          BufferedImage image = ImageIO.read(img);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          ImageIO.write(image, "jpg", baos);
          baos.flush();
          imageByte = baos.toByteArray();
          baos.close();
      } catch (IOException ex) {
          ex.printStackTrace();
      }
      return imageByte;
  }
  </code></pre>
  
  <p>My problem arrives when I try to read from the DB with the client.</p>
  
  <pre><code>public static Image bytesToImage(byte[] bytes) {
      BufferedImage image = null;
      try {
          image = ImageIO.read(new ByteArrayInputStream(bytes));
      } catch (IOException ex) {
          ex.printStackTrace();
      }
      return image;
  }
  </code></pre>
  
  <p>The image is null at this point... I searched about it and my code seems to be correct.</p>
  
  <p>I get not only an image from the server, but a whole account with GSon and when I sout my json String I get that:</p>
  
  <pre><code>{"username":"Filou","picture":[91,45,49,44,32,45,52,48,44,32,45,49,44,32,45,51,55,44,32,48,44,32,54,55,44,32,48,44,32,56,44,32,54,44,32,54...
  </code></pre>
  
  <p>In order to get the json String from the server, I use a BufferedReader and then I create a User (bean) with it:</p>
  
  <pre><code>String strRetour = reader.readLine();
  User userReturn = gson.fromJson(strRetour, User.class);
  </code></pre>
  
  <p>In the bean User there is a getter/setter for the picture and when I sout it, it looks fine:</p>
  
  <pre><code>System.out.println(Arrays.toString(userReturn.getPicture()));
  [91, 45, 49, 44, 32, 45, 52, 48, 44, 32, 45, 49, 44, 32, 45, 51, 55, 44, 32, 48, 44, 32, 54, 55, 44, 32, 48, 44, 32, 56, ...
  </code></pre>
  
  <p>So I guess the bytes array is fine?</p>
  
  <p>My question is: where is the problem? Why is my image null?</p>
  
  <p>Thanks a lot!</p>