简体   繁体   English

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

[英]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).仅供参考/上下文:我正在 windows 10 上运行 libreoffice 的便携式安装(尽管在正常安装的 mac 上遇到相同的异常)。

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).也许这很重要...我正在通过 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);
}

Writing the document编写文档

PropertyBuilder is a utility class that just builds an Array of PropertyValues for ease of use. 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);
}

The exception例外

I couldn't find a solution while searching on stackoverflow...在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?我看过 api.libreoffice.org 等的例子。我错过了什么吗? A PropertyValue?属性值?

Thank you in advance!先感谢您!

See if any of these ideas help.看看这些想法是否有帮助。

  • The URI should look like file:///E:/test/abc.odt . URI 应类似于file:///E:/test/abc.odt

  • Set the filter name to StarOffice XML (Writer) or writer8 .将过滤器名称设置为StarOffice XML (Writer)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.验证您是否有权写入文件,例如使用标准 Java 库在该位置创建文件。 Be sure the file is not locked by some other process.确保文件没有被其他进程锁定。

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

相关问题 OpenOffice,Java,将文档另存为pdf - OpenOffice,Java,Saving a document as pdf 在Scala中使用Java API查询Couchbase中的视图会引发超时异常 - Using Java API in Scala to query views in Couchbase throws timeout exception 如何使用Openoffice api和Java打开受保护的Word文档? - How to open a Protected word document using Openoffice api with java? java.lang.NullPointerException同时在Java中使用Couchdb4j API将批量文档保存到Couchdb - java.lang.NullPointerException while saving bulk documents to couchdb using couchdb4j api in java 在Java中使用JSONata会引发异常 - Using JSONata in Java throws exception 无法使用docsplit转换openoffice文档,从而导致java.lang.NoClassDefFoundError - Unable to convert openoffice documents using docsplit resulting in java.lang.NoClassDefFoundError MKS Integrity Java API在createsandbox上引发异常 - MKS Integrity Java api throws exception on createsandbox Java Spring Boot JPA - 保存到数据库时引发 NullPointer 异常 - Java Spring Boot JPA - Throws NullPointer Exception when saving to database 如何使用OpenOffice API将HTML文本放入OpenOffice文档中 - how to place HTML text into OpenOffice document using OpenOffice API 在Java中使用另一个项目的项目引发异常 - Project using another project in java throws an exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM