ODT 文件到字节数组 Java
ODT File to Byte Array Java
我正在尝试将我的 odt 文件放入 ByteArray 以将其上传到我的服务器。
我想我在这里找到了方法:How can I generate Byte array from an ODT file java .
但我遇到的一个问题是如何在 Writer 中当前打开的文件上使用它,因为我希望这一切在我按下按钮时发生?
试过这个来解决这个文件:
//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);
}
我正在尝试将我的 odt 文件放入 ByteArray 以将其上传到我的服务器。 我想我在这里找到了方法:How can I generate Byte array from an ODT file java .
但我遇到的一个问题是如何在 Writer 中当前打开的文件上使用它,因为我希望这一切在我按下按钮时发生?
试过这个来解决这个文件:
//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);
}