在 Oracle 10g 中使用 Java 1.4 将 docx 文件写入 BLOB
我正在尝试使用 Java 生成一个空白 docx 文件,添加一些文本,然后将其写入 BLOB,我可以将其返回到我们的文档处理引擎(PL/SQL 和 Java 的自定义混乱)。我必须在 Oracle 10g 中使用 1.4 JVM,所以没有 Java 1.5 的东西。我将 docx 写入本地计算机上的文件时没有问题,但当我尝试写入 BLOB 时,却收到垃圾信息。我是不是在做一些蠢事?任何帮助表示赞赏。请注意,在下面的代码中,所有 get[name]Xml() 方法都返回 org.w3c.dom.Document。
public void save(String fileName) throws Exception {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(fileName));
addEntry(zos, getDocumentXml(), "word/document.xml");
addEntry(zos, getContentTypesXml(), "[Content_Types].xml");
addEntry(zos, getRelsXml(), "_rels/.rels");
zos.flush();
zos.close();
}
public java.sql.BLOB save() throws Exception {
java.sql.Connection conn = DbUtilities.openConnection();
BLOB outBlob = BLOB.createTemporary(conn, true, BLOB.DURATION_SESSION);
outBlob.open(BLOB.MODE_READWRITE);
ZipOutputStream zos = new ZipOutputStream(outBlob.setBinaryStream(0L));
addEntry(zos, getDocumentXml(), "word/document.xml");
addEntry(zos, getContentTypesXml(), "[Content_Types].xml");
addEntry(zos, getRelsXml(), "_rels/.rels");
zos.flush();
zos.close();
return outBlob;
}
private void addEntry(ZipOutputStream zos, Document doc, String fileName) throws Exception {
Transformer t = TransformerFactory.newInstance().newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
t.transform(new DOMSource(doc), new StreamResult(baos));
ZipEntry ze = new ZipEntry(fileName);
byte[] data = baos.toByteArray();
ze.setSize(data.length);
zos.putNextEntry(ze);
zos.write(data);
zos.flush();
zos.closeEntry();
}
I'm trying to generate a blank docx file using Java, add some text, then write it to a BLOB that I can return to our document processing engine (a custom mess of PL/SQL and Java). I have to use the 1.4 JVM inside Oracle 10g, so no Java 1.5 stuff. I don't have a problem writing the docx to a file on my local machine, but when I try to write to BLOB, I'm getting garbage. Am I doing something dumb? Any help is appreciated. Note in the code below, all the get[name]Xml() methods return an org.w3c.dom.Document.
public void save(String fileName) throws Exception {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(fileName));
addEntry(zos, getDocumentXml(), "word/document.xml");
addEntry(zos, getContentTypesXml(), "[Content_Types].xml");
addEntry(zos, getRelsXml(), "_rels/.rels");
zos.flush();
zos.close();
}
public java.sql.BLOB save() throws Exception {
java.sql.Connection conn = DbUtilities.openConnection();
BLOB outBlob = BLOB.createTemporary(conn, true, BLOB.DURATION_SESSION);
outBlob.open(BLOB.MODE_READWRITE);
ZipOutputStream zos = new ZipOutputStream(outBlob.setBinaryStream(0L));
addEntry(zos, getDocumentXml(), "word/document.xml");
addEntry(zos, getContentTypesXml(), "[Content_Types].xml");
addEntry(zos, getRelsXml(), "_rels/.rels");
zos.flush();
zos.close();
return outBlob;
}
private void addEntry(ZipOutputStream zos, Document doc, String fileName) throws Exception {
Transformer t = TransformerFactory.newInstance().newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
t.transform(new DOMSource(doc), new StreamResult(baos));
ZipEntry ze = new ZipEntry(fileName);
byte[] data = baos.toByteArray();
ze.setSize(data.length);
zos.putNextEntry(ze);
zos.write(data);
zos.flush();
zos.closeEntry();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来问题出在我们的文档处理引擎中。它期待一个压缩的 docx。很高兴我们很好地记录了我们的代码。
无论如何,感谢所有关注我问题的人。
It looks like the problem was in our document processing engine. It was expecting a zipped docx. Glad we document our code well.
Anyway, thanks to everyone who looked at my problem.