簡體   English   中英

從Oracle blob字段中提取文件;

[英]Extracting files from Oracle blob fields;

我正在嘗試使用在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;
                    }

 //...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM