【问题标题】:How to store/retrieve an Apache-poi workbook object into/from Oracle blob column?如何在 Oracle blob 列中存储/检索 Apache-poi 工作簿对象?
【发布时间】:2017-01-19 01:06:13
【问题描述】:

Java 1.7、Apache-poi-3.8、JDBC

要求: 1) 创建一个包含完整数据(来自数据库)的工作簿对象。无需在服务器上创建任何文件。

HSSFWorkbook wb = new HSSFWorkbook(); // this object I want to store in DB blob column, without any file creation at server.

insertDataToCells(wb, "output", data); // this method will insert data in cells of workbook.

**//TODO code to store wb in blob column.** // How to store ?

2) 从 blob 中检索数据(工作簿)并将其转换为 excel 并发送到邮件附件中

// TODO How to retrive

以下方法:

byte[] byteArray = wb.getBytes();
InputStream is = new ByteArrayInputStream(byteArray);
ps.setBinaryStream(1, is, 1000000); // Here i am having some confusion 
ps.execute // Successfully stored in blob column.

检索时出错文件已损坏。

【问题讨论】:

    标签: java jdbc apache-poi


    【解决方案1】:

    您需要正确设置流中的字节数,因此在您的情况下应该是:

    byte[] byteArray = wb.getBytes();
    ps.setBinaryStream(1, new ByteArrayInputStream(byteArray), byteArray.length);
    ...
    

    然后要以InputStream 的形式检索列的内容,请使用getBinaryStream(String columnLabel)getBinaryStream(int columnIndex)

    【讨论】:

    • true :) 我给出的长度是 1000000,因为它正在变成损坏的文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    相关资源
    最近更新 更多