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
Expand|Select|Wrap|Line Numbers
- PreparedStatement ps =
- con.prepareStatement( "UPDATE tk_tablename SET tk_filecolumnname = ? WHERE primarykey = '"
- + fileAttachment.getPrimaryKey() + "';" );
- 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();
- }
But while reading the file on server using below code
Expand|Select|Wrap|Line Numbers
- ResultSet rs =
- stmt.executeQuery( "SELECT tk_filecolumnname FROM tk_tablename WHERE primarykey = '"
- + fileAttachment.getPrimaryKey() + "';" );
- if ( rs != null && rs.next() )
- {
- InputStream fileInputStream = rs.getBinaryStream( 1 );
- Workbook workbook = WorkbookFactory.create( fileInputStream ); // apache POI code to read a xlsx file.
- rs.close();
- return file;
- }
Expand|Select|Wrap|Line Numbers
- java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
Expand|Select|Wrap|Line Numbers
- ResultSet.getBinaryStream()
Please help or share your knowledge.
thanks,
Amit.