title: Get binary image data from postgres database and save in folder
body: <p>I want to save all the image in a folder which already saved in database as 
  byte array.</p>
  
  <p>After successfully saving 200+ image my code shows error.</p>
  
  <p>Details of my code and error described below : </p>
  
  <p><strong>Database field type</strong></p>
  
  <pre><code>photograph bytea
  </code></pre>
  
  <p><strong>Domain Class</strong></p>
  
  <pre><code>private Byte[] photograph
  </code></pre>
  
  <p>What I have tried so far : </p>
  
  <pre><code>import java.awt.Graphics2D;
  import java.awt.Image;
  import java.awt.color.CMMException;
  import java.awt.image.BufferedImage;
  import java.io.ByteArrayInputStream;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.sql.DriverManager;
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import javax.imageio.ImageIO;
  import org.postgresql.largeobject.LargeObjectManager;
  
  public class Test {
  
      public static void main(String[] argv) throws SQLException, IOException {
  
          System.out.println("-------- PostgreSQL "
                  + "JDBC Connection Testing ------------");
  
          try {
  
              Class.forName("org.postgresql.Driver");
  
          } catch (ClassNotFoundException e) {
  
              System.out.println("Where is your PostgreSQL JDBC Driver? "
                      + "Include in your library path!");
              e.printStackTrace();
              return;
  
          }
  
          System.out.println("PostgreSQL JDBC Driver Registered!");
  
          Connection connection = null;
  
          try {
  
              connection = DriverManager.getConnection(
                      "jdbc:postgresql://127.0.0.1:5432/an?currentSchema=an", "postgres"
                                                  + "",
                      "123456");
  
          } catch (SQLException e) {
  
              System.out.println("Connection Failed! Check output console");
              e.printStackTrace();
              return;
  
          }
  
          if (connection != null) {
              System.out.println("You made it, take control your database now!");
                          //start
                          String query = "my_query";
                          PreparedStatement pst = connection.prepareStatement(query);
                          ResultSet rs = pst.executeQuery();
                          int i=0;
                          while (rs.next()) {
                              i++;
                              System.out.println("Saving ... :  "+i);
                              // convert byte array back to BufferedImage
                          if(null == rs.getString(1)){
                              continue;
                          }    
              InputStream in = new ByteArrayInputStream(rs.getBytes(1));
              BufferedImage bImageFromConvert = ImageIO.read(in);
                          if(null == bImageFromConvert){
                              continue;
                          }
                          OutputStream out = new FileOutputStream("e:/images/"+rs.getString(2)+".jpg");
  
              ImageIO.write(bImageFromConvert, "jpg", out);
                          }
                          //end
  
  
  
  
          } else {
              System.out.println("Failed to make connection!");
          }
      }
  
  
  
  
  
  }
  </code></pre>
  
  <p>It works fine until error occurs . How to overcome this problem .
  I want to save all images and if i cant then just skip to next image 
  thats what I want.</p>
  
  <p><strong>Error :</strong> </p>
  
  <pre><code>Exception in thread "main" javax.imageio.IIOException: Unsupported Image Type
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1068)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1039)
      at javax.imageio.ImageIO.read(ImageIO.java:1448)
      at javax.imageio.ImageIO.read(ImageIO.java:1352)
      at test.Test.main(Test.java:84)
  C:\Users\Shifat\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
  BUILD FAILED (total time: 20 seconds)
  </code></pre>