I have a Microsoft Access database with an OLE Object field holding a Microsoft Word document.
I have tried to find code to retrieve the file saved in the OLE Object, so that the user can download it from a button in my JavaFx application, but I had no success.
I have the following but I don't know what to do after this. Also, inputStream is always null.
InputStream inputStream = res.getBinaryStream(6);
解决方案
You seem to be on the right track with regard to getting the binary data out of the database. The following code works for me with UCanAccess 3.0.0 under Java 7, where [Doc] is an OLE Object field in an Access table:
String sql = "SELECT Doc FROM OleTest WHERE ID=1";
try (Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql)) {
rs.next();
InputStream inputStream = rs.getBinaryStream(1);
File f = new File("C:/Users/Gord/Desktop/thing.bin");
Files.copy(
inputStream,
f.toPath(),
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
}
Now the question is whether the field contained a Word document
in raw binary format, or
as a true OLE ("wrapped") object.
If the field contained a document in raw binary format then we could just rename the file to .docx and open it directly in Word.
However, in my case it was stored as a "wrapped" OLE Object because I had imbedded the document into the table using "Insert object..." in Access itself. Therefore the .docx (Word) document, which in raw form looks like this ...
... is extracted from the database with its "OLE wrapper" around it:
If we search down through the OLE data from the database we can see the beginning of the raw binary data, in this case at offset 0xA57:
So, unfortunately we cannot simply save the OLE binary data into a file and then open that file directly in Word because it is not a valid Word file.
Removing OLE "wrappers" can be tricky. Some file formats are designed to ignore extraneous bytes at the end of a file, so approaches like the one described in this answer (which just removes the "front part" of the OLE wrapper) can be used with image file formats like BMP, JPEG, etc.. Unfortunately, Word documents are much less forgiving of "junk" at the end of the file so just removing the "front part" of the OLE wrapper can still result in a file that Word cannot open.