【发布时间】:2015-01-13 18:17:06
【问题描述】:
我正在使用 apache POI 读取 xlsx 文件并上传数据库中的数据。我必须在 Jboss 上运行的调度程序(在预定时间)中完成这项工作。由于调度程序与上传的一个文件在不同的服务器上运行,我使用 postgres bytea 数据类型使用以下代码将文件保存在数据库中 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();
}
一切正常,文件已保存在数据库中。
但是在使用下面的代码读取服务器上的文件时
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;
}
它给出以下错误,
java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
我知道要读取 xlsx 文件,POI 需要由 OOXML 支持的流。但是为什么
ResultSet.getBinaryStream()
方法返回的输入流与创建文件时保存的输入流不同。
请帮助或分享您的知识。
谢谢, 阿米特。
【问题讨论】:
-
读取数据很可能不会返回 same 输入流(实例),尤其是。因为在 db 中没有关于使用哪个流的数据(它只是二进制文件数据)。
标签: java apache-poi