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

时间:2013-10-30 14:52:29

标签: java database oracle file blob

我正在尝试从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());
                    }
                }
//...

1 个答案:

答案 0 :(得分:1)

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