【问题标题】:Extracting blobs to files: some files are missing data将 blob 提取到文件:某些文件缺少数据
【发布时间】:2013-11-10 06:16:31
【问题描述】:

我正在尝试从 oracle 数据库中提取 TIF 文件。从 blob 转换的大多数文件都可以正常工作,但有些文件似乎丢失了几千字节。

例如,当我使用 SQL GUI 提取文件时,我得到一个大小为 912,390 字节的 TIF 文件。当我使用我的方法时,我得到一个大小为 909,312 字节的文件。缺少的 3078 字节意味着我无法打开文件。我在做什么导致文件不完整?

//...
 if ((FileOutDir != null) && (blobSQL != null) && (conn != null)) {
        PreparedStatement selBlobs = null;
        OutputStream fos = null;

        if (conn != null) {
            if (blobSQL != null) {
                try {

                    selBlobs = conn.prepareStatement(blobSQL);
                    ResultSet rs = selBlobs.executeQuery();

                    Blob blob = null;
                    InputStream is = null;

                    int b = 0;
                    int cols = rs.getMetaData().getColumnCount();

                    String filepath = FileOutDir;

                    while (rs.next()) {

                        blob = rs.getBlob(1);
                        is = blob.getBinaryStream();
                        filepath += "/";

                        for (int c = 2; c <= cols; c++) {
                            filepath += rs.getObject(c).toString() + "_";
                        }
                        filepath = filepath.substring(0,
                                filepath.length() - 1);
                        filepath += fileSuffix;

                        fos = new BufferedOutputStream(new FileOutputStream(filepath));

                        while ((b = is.read()) != -1) {
                            fos.write(b);
                        }

                        filepath = FileOutDir;
                        b = 0;
                    }

                } catch (Exception e) {
                    JOptionPane.showMessageDialog(gui, e.toString());
                } finally {
                    try {
                        selBlobs.close();
                        fos.close();
                    } catch (Exception e2) {
                        System.out
                                .println("Problem closing BlobToFile connections: "
                                        + e2.toString());
                    }
                }
//...

【问题讨论】:

  • 您可以尝试在fos.close(); 之前添加fos.flush(); 看看是否有帮助?
  • 这似乎已经解决了问题,但我必须尝试使用​​更大的设置来确保。如果它适用于 10,000 行,我会留下答案。再次感谢!
  • 我会将其添加为答案,以便您在有帮助时关闭问题。

标签: java database oracle file blob


【解决方案1】:

尝试在fos.close(); 之前添加fos.flush();,看看是否有帮助。

【讨论】:

  • 仍然适用于 10, 000 行。再次感谢 Przemyslaw Kruglej,感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-02
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多