【问题标题】:How to get blob file name?如何获取 blob 文件名?
【发布时间】:2015-03-26 02:46:27
【问题描述】:

我有一个返回 Blob 的 pl/sql 过程。我是这样称呼它的:

CallableStatement cstmt = null;
Blob blob = null;
cstmt = con.prepareCall("{? = call billing.PREQUEST.GetAttachment(?)}");
                                cstmt.registerOutParameter(1,
                                        OracleTypes.BLOB);
cstmt.setInt(2, id);
cstmt.execute();
blob = cstmt.getBlob(1);

现在我想从 blob 中获取文件名。我该怎么做?

【问题讨论】:

  • 保存了什么样的文件?通常大多数文件没有嵌入其中的名称...通常您会将元数据与二进制字节(文件名等)一起存储在单独的列或表中

标签: java oracle plsql


【解决方案1】:

BLOB 列不存储文件名。您需要添加一个单独的 VARCHAR2 类型的列来存储原始文件名。

如果您要处理已经包含大量数据的现有数据库,那么您就不走运了。文件名未存储,无法恢复。

【讨论】:

    【解决方案2】:

    您需要将Blob 更改为File。试试这个代码:

    Blob blob = cstmt.getBlob(column);
    InputStream in = blob.getBinaryStream();
    OutputStream out = new FileOutputStream("OutputFile");
    byte[] buff = new byte[4096]; 
    int length = 0;
    
    while ((length = in.read(buff)) != -1) {
        out.write(buff, 0, length );
    }
    
    // From here, read in the File and just call "getName()"
    // Also, make sure to call to close out and in
    

    【讨论】:

    • 这不会为 OP 提供已存储为字节流的原始文件名
    • 我同意File 列类型可能会有所帮助。但我看不出提供的代码与您的答案有什么关系。
    • 我提供了一种创建文件的方法,然后从那里获取名称。我不想提供整个解决方案。
    猜你喜欢
    • 2019-03-22
    • 2020-08-17
    • 2014-10-06
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 2019-11-21
    相关资源
    最近更新 更多