简体   繁体   English

从Oracle blob字段中提取文件;

[英]Extracting files from Oracle blob fields;

I am trying to extract a file from a blob using the values found in the fields following the blob column. 我正在尝试使用在Blob列后面的字段中找到的值从Blob中提取文件。 My solution works, but it is rather slow. 我的解决方案有效,但是速度很慢。

I extracted 169MB(727 different files) in about 1 hour. 我在大约1小时的时间内提取了169MB(727个不同的文件)。 That's about 12 files a minute. 每分钟大约12个文件。 most of the files are usually between 5KB and 50KB but can sometimes be as big as 2MB. 大多数文件通常在5KB至50KB之间,但有时可能高达2MB。 I am working with a local Oracle database. 我正在使用本地Oracle数据库。

Is there anything I could do to make my code more efficient? 我有什么办法可以使我的代码更高效? If not, what other factors might affect the speed of the process? 如果不是,还有哪些其他因素可能会影响流程速度? Here is the method's code: 这是方法的代码:

public void beginExtraction(String FileOutDir, String blobSQL,
        String fileSuffix, Connection conn) {

    if ((FileOutDir != null) && (blobSQL != null) && (conn != null)) {
        PreparedStatement selBlobs = null;
        FileOutputStream fos = null;

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

                    selBlobs = conn.prepareStatement(blobSQL);
                    ResultSet rs = selBlobs.executeQuery();
                    int cols = rs.getMetaData().getColumnCount();

                    while (rs.next()) {

                        Blob blob = rs.getBlob(1);
                        InputStream is = blob.getBinaryStream();

                        String filepath = "";

                        filepath += FileOutDir + "/";

                        for (int c = 2; c <= cols; c++) {
                            filepath += rs.getObject(c).toString() + "_";
                        }

                        filepath = filepath.substring(0,
                                filepath.length() - 1);
                        filepath += fileSuffix;
                        fos = new FileOutputStream(filepath);

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

                    }

                    selBlobs.close();
                    fos.close();

                } catch (Exception e) {
                    JOptionPane.showMessageDialog(gui, e.toString());
                }
            }
        }
    } else {
        if (conn == null) {
            JOptionPane.showMessageDialog(gui,
                    "You have not selected a database.");
        } else {
            if (FileOutDir == null) {
                JOptionPane.showMessageDialog(gui,
                        "You have not chosen a directory for your files.");
            } else {
                if (blobSQL == null) {
                    JOptionPane.showMessageDialog(gui,
                            "Please insert an SQL statement.");

                }
            }
        }
    }
}

Changing to a buffered output made the process exponentially faster. 更改为缓冲输出可使过程成倍增长。 I was able to export the 727 files in under a minute. 我在一分钟内就能导出727个文件。 Here the new code: 这里是新代码:

//...

                    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;
                    }

 //...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM