title: Not able to read xlsx file saved in postgres database (as bytea) using apache POI
body: <p>I am using apache POI to read a xlsx file and upload the data in the database. I have to do this work in a scheduler(at scheduled time) running on Jboss. As scheduler is running on different server from the one file was uploaded, I am using postgres bytea datatype to save the file in database using below code
      PreparedStatement ps = con.prepareStatement( "UPDATE tk_tablename SET tk_filecolumnname = ? WHERE primarykey = '"
                                  + fileAttachment.getPrimaryKey() + "';" );</p>
  
  <pre><code>FilePathAssociation filePathAssociation = fileAttachment.getFilePathAssociation();
  
  if ( filePathAssociation != null )
  
  {
  
  File blobFile = new File( filePathAssociation.getPhysicalFilePath() );
     FileInputStream fis = new FileInputStream( blobFile );
     ps.setBinaryStream( 1, fis, (int)blobFile.length() );
     ps.executeUpdate();
     ps.close();
     fis.close();
  }
  </code></pre>
  
  <p>This is working fine and the file is saved in database.</p>
  
  <p>But while reading the file on server using below code</p>
  
  <pre><code>ResultSet rs =
                          stmt.executeQuery( "SELECT tk_filecolumnname FROM tk_tablename WHERE primarykey = '"
                              + fileAttachment.getPrimaryKey() + "';" );
                      if ( rs != null &amp;&amp; rs.next() )
                      {
                          InputStream fileInputStream = rs.getBinaryStream( 1 );
                          Workbook workbook = WorkbookFactory.create( fileInputStream ); // apache POI code to read a xlsx file.
                          rs.close();
                          return file;
                      }
  </code></pre>
  
  <p>It is giving below error,</p>
  
  <pre><code>java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
  </code></pre>
  
  <p>I know to read a xlsx file, POI needs a stream backed by OOXML. But why</p>
  
  <pre><code>ResultSet.getBinaryStream()
  </code></pre>
  
  <p>method is not returning the same input stream that was saved while creating file.</p>
  
  <p>Please help or share your knowledge.</p>
  
  <p>thanks,
  Amit. </p>