我正在尝试使用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.");
}
}
}
}
}
答案 0 :(得分:1)
更改为缓冲输出使得该过程呈指数级增长。我能够在一分钟内导出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;
}
//...