Extracting blobs to files: some files are missing data

风流意气都作罢 提交于 2019-12-11 19:18:09

问题


I am trying to extract a TIF file from an oracle database. Most of the files that are converted from a blob work fine, but some files seem to be missing a few kilobytes.

For example, when I extract a file using the SQL GUI I get a TIF file that is 912,390 bytes in size. When I use my method, I get a file that is 909,312 bytes in size. The missing 3078 bytes means I can't open the file. What am I doing that is causing the files to be incomplete?

//...
 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:


Try adding fos.flush(); before fos.close(); and see if it helps.



来源:https://stackoverflow.com/questions/19686232/extracting-blobs-to-files-some-files-are-missing-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!