简体   繁体   中英

Saving documents using OpenOffice java api throws Exception

FYI / Context: I am running a portable installation of libreoffice on windows 10 (getting the same exception on a mac with normal installation though).

Reading the document

Maybe this is important... I am reading the document through an InputStream, because the other method fails due to a different exception (probably a story for another time).

public XComponent openFileViaStream(File file) throws CommandAbortedException, Exception {
    Object fileAccess = this.componentFactory.createInstanceWithContext(SimpleFileAccessClass, this.context);
    XSimpleFileAccess xSimpleFileAccess = (XSimpleFileAccess) UnoRuntime.queryInterface(XSimpleFileAccess.class,
            fileAccess);
    XStream xInputStream = xSimpleFileAccess.openFileReadWrite(file.getAbsolutePath());
    PropertyValue[] loadProps = new PropertyBuilder().add("InputStream", xInputStream).build();

    return loader.loadComponentFromURL("private:stream", "_blank", 0, loadProps);
}

Writing the document

PropertyBuilder is a utility class that just builds an Array of PropertyValues for ease of use.

public void save(Object storeMe, File destination) throws IOException, MalformedURLException {
    //@formatter:off
    PropertyValue[] propertyValue = new PropertyBuilder()
            .add("Overwrite", Boolean.TRUE)
            .add("FilterName", "StarOffice XML")
            .build();
    //@formatter:on
        
    XStorable2 st = UnoRuntime.queryInterface(XStorable2.class, storeMe);
    

    // already tried
    // st.storeAsURL(destination.toURI().toURL().toString(), propertyValue);
    // st.storeToURL(destination.toURI().toString(), propertyValue);
    // st.storeToURL(destination.toURI().toURL().toString(), propertyValue);
        
    st.storeAsURL(destination.toURI().toString(), propertyValue);
}

The exception

I couldn't find a solution while searching on stackoverflow...

 com.sun.star.task.ErrorCodeIOException: SfxBaseModel::impl_store <file:/E:/test/abc.odt> failed: 0x81a(Error Area:Io Class:Parameter Code:26)
    at com.sun.star.lib.uno.environments.remote.Job.remoteUnoRequestRaisedException(Job.java:173)
    at com.sun.star.lib.uno.environments.remote.Job.execute(Job.java:139)
    at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:334)
    at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:303)
    at com.sun.star.lib.uno.environments.remote.JavaThreadPool.enter(JavaThreadPool.java:87)
    at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge.sendRequest(java_remote_bridge.java:636)
    at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.request(ProxyFactory.java:146)
    at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.invoke(ProxyFactory.java:128)
    at com.sun.proxy.$Proxy10.storeAsURL(Unknown Source)
    at DocumentHandler.save(DocumentHandler.java:54)
    at Main.test(Main.java:14)
    at Main.main(Main.java:19)

I really have no idea what I am doing wrong. I've looked at examples from api.libreoffice.org etc. Am I missing something? A PropertyValue?

Thank you in advance!

See if any of these ideas help.

  • The URI should look like file:///E:/test/abc.odt .

  • Set the filter name to StarOffice XML (Writer) or writer8 . Or don't set it at all; pass one property instead of two.

  • Verify you have the authorization to write to the file, for example by using standard Java libraries to create a file in that location. Be sure the file is not locked by some other process.

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