Java action example to store file document on local filesystem

0
Is there somebody that can help me with a java action example to store a file document object on the local file system with a given path and filename? A already searched the forum and appstore, but apparently I am the first one that needs this. Any help to get me started would be appreciated! --Edit-- Ok, I have now the below code in my custom java action that accepts a path, filename and FileDocument as input. It already creates the file on the local filesystem succesfully. Only the file is, offcourse, empty and I can't found out how to put an inputstream from the FileDocument object in the local file. Anybody can help me with that specific part? // This file was generated by Mendix Business Modeler. // // WARNING: Only the following code will be retained when actions are regenerated: // - the import list // - the code between BEGIN USER CODE and END USER CODE // - the code between BEGIN EXTRA CODE and END EXTRA CODE // Other code you write will be lost the next time you deploy the project. // Special characters, e.g., é, ö, à, etc. are supported in comments. package general.actions; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.webui.CustomJavaAction; import com.mendix.core.Core; import com.mendix.systemwideinterfaces.core.IMendixObject; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; /** * Use this procedure to create a file on the local filesystem. */ public class CreateFile extends CustomJavaAction<Boolean> { private String mapPad; private String bestandNaam; private IMendixObject __fileDocument; private system.proxies.FileDocument fileDocument; public CreateFile(IContext context, String mapPad, String bestandNaam, IMendixObject fileDocument) { super(context); this.mapPad = mapPad; this.bestandNaam = bestandNaam; this.__fileDocument = fileDocument; } @Override public Boolean executeAction() throws Exception { this.fileDocument = __fileDocument == null ? null : system.proxies.FileDocument.initialize(getContext(), __fileDocument); // BEGIN USER CODE File f = null; if(mapPad == null || mapPad.length() == 0){ Core.getLogger("CreateFile").info("No Path Specified!!"); return false; } if(bestandNaam == null || bestandNaam.length() == 0){ Core.getLogger("CreateFile").info("No FileName Specified!!"); return false; } try{ f = new File(mapPad + bestandNaam); f.createNewFile(); } catch(Exception e){ Core.getLogger("CreateFile").info("An error has occured!!"); return false; } Core.getLogger("CreateFile").info("C"); return true; // END USER CODE } /** * Returns a string representation of this action */ @Override public String toString() { return "CreateFile"; } // BEGIN EXTRA CODE // END EXTRA CODE }
asked
1 answers
6

Rene,

Here is a bit of (pseudo)code the should help you.(replace the stuff between <> with the necessary values and you should be good to go.

    InputStream inputStream = null;
OutputStream outputStream = null;

try {
    // read the fildocument into InputStream
    inputStream = Core.getFileDocumentContent(context, fileDocument);

    // write the inputStream to a FileOutputStream
    outputStream = 
                new FileOutputStream(new File("<path to file including filename>"));

    int read = 0;
    byte[] bytes = new byte[1024];

    while ((read = inputStream.read(bytes)) != -1) {
        outputStream.write(bytes, 0, read);
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (inputStream != null) {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace(); /* or Core.getLogger("<lognode>").error("<message>"); */
        }
    }
    if (outputStream != null) {
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();/* or Core.getLogger("<lognode>").error("<message>"); */
        }

    }
}
answered