[英]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 上遇到相同的异常)。
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);
}
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);
}
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.