将blob提取到文件:某些文件缺少数据 [英] Extracting blobs to files: some files are missing data
问题描述
我试图从oracle数据库中提取一个TIF文件。从blob转换的大多数文件工作正常,但一些文件似乎缺少几个千字节。
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.
例如,当我使用SQL GUI提取文件时,我得到一个大小为912,390字节的TIF文件。当我使用我的方法,我得到一个大小为909,312字节的文件。缺少3078字节表示我无法打开文件。我这样做会导致文件不完整?
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());
}
}
//...
推荐答案
尝试在 fos.close();
之前添加 fos.flush();
看看它是否有帮助。
Try adding fos.flush();
before fos.close();
and see if it helps.
这篇关于将blob提取到文件:某些文件缺少数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!