java - 从 Oracle blob 字段中提取文件;

标签 java database oracle blob

我尝试使用 Blob 列后面的字段中找到的值从 Blob 中提取文件。我的解决方案有效,但速度相当慢。

我在大约 1 小时内提取了 169MB(727 个不同的文件)。每分钟大约 12 个文件。 大多数文件通常在 5KB 到 50KB 之间,但有时可能大至 2MB。我正在使用本地 Oracle 数据库。

我可以做些什么来提高我的代码效率吗?如果不是,还有哪些其他因素可能会影响该过程的速度?这是该方法的代码:

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.");

                }
            }
        }
    }
}

最佳答案

更改为缓冲输出使该过程呈指数级加快。我能够在一分钟内导出 727 个文件。这是新代码:

//...

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

 //...

关于java - 从 Oracle blob 字段中提取文件;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19664434/

相关文章:

ruby-on-rails - Amazon SimpleDB 是我数据的不错选择吗?

oracle - 计算 oracle varchar 列值中给定符号后的字符数

c++ - Oracle SQL 删除位于数组/C 数组/数据结构中的值

java - 尝试使用 Java 更新 SQL 表

java - 从 getter 方法获取数据而不是传入 Bundle

MySql - 如何根据返回的行数有条件地 ORDER BY?

java - 不使用 putAll() 从一个 HashMap 复制到另一个 HashMap

database - 将 SQLite 数据库文件放在 Azure 应用服务中的哪里?

java - Oracle 在处理 java 应用程序时由对等方重置连接

java - 将面板布局设置为 null 时出现 NullPointerException?