[英]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.