0

I am Trying to get my odt file into a ByteArray for Uploadting it to my server. I Think i found the way how to do it here :How can I generate Byte array from an ODT file java .

But the one Problem I have is how can I use this on the File that is currently open in the Writer because I want that all this happens when I press a Button?

Tried this to Adress this File:

            //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);

But this does not seem to work.

So maybe you can tell me how I can Adress the File :)

Community
  • 1
  • 1
FreeKill
  • 13
  • 4

1 Answers1

0

Save the ODT file to disk first. The following code works on my machine:

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;

In the main routine:

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);
}
Jim K
  • 12,824
  • 2
  • 22
  • 51
  • Ok thanks. But isnt there a way I can do this without saving the File on my disk ? this would be much better. Found the Method readFileToByteArray(); But cannot use it on my xtextdocument. – FreeKill Feb 08 '16 at 11:46
  • As far as I know, the only way is to save the file. From the [readFileToByteArray() documentation](https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#readFileToByteArray(java.io.File)): "Reads the contents of a file into a byte array." It takes a [java.io.File](http://docs.oracle.com/javase/6/docs/api/java/io/File.html?is-external=true) parameter, so it is on the disk. – Jim K Feb 08 '16 at 13:32
  • Anyway why would it be "much better" not to save to disk? For that matter, why do you need a byte array? [XStorable](https://www.openoffice.org/api/docs/common/ref/com/sun/star/frame/XStorable.html#storeToURL) should accept a server address. – Jim K Feb 08 '16 at 13:57
  • Ok thank you but I tried to safe the file but it does not work :( Maybe I use it the wront way `XModel xDocModel = this.frame.getController().getModel(); XTextDocument xtextdocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xDocModel); XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, xtextdocument); PropertyValue[] storeProbs = new PropertyValue[1]; xStorable.storeToURL("C:\\Windows\\Temp\\Test.odt", storeProbs); ` – FreeKill Feb 09 '16 at 07:25
  • I modified my answer to use code that is similar to what you tried. – Jim K Feb 09 '16 at 14:10
  • Thank you. I am Getting an Error Saying no suitable constructor for URL(String) – FreeKill Feb 10 '16 at 07:48
  • And I need the Byte Array cause our Server needs it. – FreeKill Feb 10 '16 at 09:08
  • Updatet my Code so you can see what i Did.. I am having issues that Files.readAllBytes() will not work and maks my variable Test – FreeKill Feb 10 '16 at 09:46
  • Did it give an error message? See my updated code, which compiled and ran successfully. See if it works to save to your desktop, since there may be some problem with the Temp folder. – Jim K Feb 10 '16 at 16:26
  • Yeah was my bad I did something wrong but now it works perfectly fine :) Thank you very Much :) – FreeKill Feb 11 '16 at 06:46