title: PostgreSQL getting image from database
body: <p>I have some Java code which get's an image from the database and saves it out to a file, however, when I open it in all programs but Photoshop I get an error similar to:</p>
  
  <p>"It may be damaged or use a file format that Preview doesn’t recognize." - This paticular one is from preview on MAC. The database i'm pulling from is PostgreSQL and the column is a bytea.</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
          Connection conn = null;
          try {
              conn = ConnectionManager.getConnection();
  
              String sql = "SELECT content from asset ";
              PreparedStatement stmt = conn.prepareStatement(sql);
              ResultSet resultSet = stmt.executeQuery();
              while (resultSet.next()) {
  
                  File image = new File("/Users/esklein/testing"+UUID.randomUUID()+".jpg");
                  FileOutputStream fos = new FileOutputStream(image);
  
                  byte[] buffer = new byte[256];
  
                  //
                  // Get the binary stream of our BLOB data
                  //
                  InputStream is = resultSet.getBinaryStream("content");
                  while (is.read(buffer) &gt; 0) {
                      fos.write(buffer);
                  }
  
                  fos.close();
              }
          } catch (SQLException e) {
              e.printStackTrace();
          } finally {
              if (conn != null &amp;&amp; !conn.isClosed()) {
                  conn.close();
              }
          }
      }
  </code></pre>
  
  <p>Any ideas why this isn't spitting out an image which can be opened in any file? Thanks</p>