FreeKill February 2016

ODT File to Byte Array Java

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 :)

Answers


Jim K February 2016

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

Post Status

Asked in February 2016
Viewed 2,561 times
Voted 4
Answered 1 times

Search




Leave an answer