将 Blob 提取到文件:某些文件缺少数据



我正在尝试从预言机数据库中提取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();,看看是否有帮助。

最新更新