繁体   English   中英

使用 OpenOffice java api 保存文档会引发异常

[英]Saving documents using OpenOffice java api throws Exception

仅供参考/上下文:我正在 windows 10 上运行 libreoffice 的便携式安装(尽管在正常安装的 mac 上遇到相同的异常)。

阅读文档

也许这很重要...我正在通过 InputStream 阅读文档,因为另一种方法由于不同的异常而失败(可能是另一个故事)。

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

编写文档

PropertyBuilder 是一个实用程序 class,它只是构建一个 PropertyValues 数组以便于使用。

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

例外

在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)

我真的不知道我做错了什么。 我看过 api.libreoffice.org 等的例子。我错过了什么吗? 属性值?

先感谢您!

看看这些想法是否有帮助。

  • URI 应类似于file:///E:/test/abc.odt

  • 将过滤器名称设置为StarOffice XML (Writer)writer8 或者根本不设置它; 传递一个属性而不是两个。

  • 验证您是否有权写入文件,例如使用标准 Java 库在该位置创建文件。 确保文件没有被其他进程锁定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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