简体   繁体   中英

Writing a docx file to a BLOB using Java 1.4 inside Oracle 10g

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();
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM