access ole java,从MS Access中提取OLE对象(Word文档)

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 ...

VlaVM.png

... is extracted from the database with its "OLE wrapper" around it:

CBjZq.png

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:

VMYP3.png

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值