ODT 文件到字节数组 Java



我正在尝试将我的 odt 文件放入 ByteArray 中,以便将其上传到我的服务器。我想我在这里找到了如何做到这一点的方法:如何从 ODT 文件 java 生成字节数组。

但是我遇到的一个问题是如何在当前在编写器中打开的文件上使用它,因为我希望所有这些在按下按钮时发生?

尝试这个来解决这个文件:

            //Abspeichern des Documents im Temp Ordner
            String storeUrl = "file:///C:/Windows/Temp/Test.odt";
            XModel xDocModel = this.frame.getController().getModel();
            XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xDocModel);
            XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
            PropertyValue[] storeProps = new PropertyValue[1];
            storeProps[0] = new PropertyValue();
            storeProps[0].Name = "Overwrite";
            storeProps[0].Value = new Boolean(true);
            try {
                xStorable.storeToURL(storeUrl, storeProps);
            } catch (com.sun.star.io.IOException ex) {
                Logger.getLogger(OptionPageDemo.class.getName()).log(Level.SEVERE, null, ex);
            }
            //Konvertieren in byte[]
            Path Test = Paths.get("C:/Windows/Temp/Test.odt");
            byte[] data = Files.readAllBytes(Test);

但这似乎行不通。

所以也许你可以告诉我如何解决文件:)

首先将 ODT 文件保存到磁盘。 以下代码适用于我的机器:

import com.sun.star.frame.XStorable;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

在主例程中:

String storeUrl = "file:///C:/Users/JimStandard/Desktop/Test.odt";
XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
    XStorable.class, xTextDocument);
PropertyValue[] storeProps = new PropertyValue[1];
storeProps[0] = new PropertyValue();
storeProps[0].Name = "Overwrite";
storeProps[0].Value = new Boolean(true);
try {
    xStorable.storeToURL(storeUrl, storeProps);
} catch (com.sun.star.io.IOException ex) {
    ex.printStackTrace(System.err);
    System.exit(1);
}  
try {
    URL url = new URL(storeUrl);  // this is 
    Path testPath = Paths.get(url.toURI());
    byte[] data = Files.readAllBytes(testPath);
    System.out.println("Length = " + data.length);
} catch (java.io.IOException ex) {
    ex.printStackTrace(System.err);
    System.exit(1);
} catch (URISyntaxException ex) {
    ex.printStackTrace(System.err);
    System.exit(1);
}

最新更新