问题
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