File from file document java action or custom java action for dowloading the documents does not work on environments

0
Hello,   On click of a button I need to implement downloading of List of FileDocuments to specific path (example: C:\New folder).  I tried it with File from file document java action from community commons and it works when I run the project locally but it does not work when I deploy it on Acceptance. Error that occurs is: Caused by: com.mendix.modules.microflowengine.MicroflowException: com.mendix.systemwideinterfaces.MendixRuntimeException: java.io.FileNotFoundException: C:\New folder\testdocument.docx (Permission denied)   I also tried with custom Java action and in this case it also works locally but on environment I get the error: java.io.FileNotFoundException C:\New folder/F_5531512519_-.docx (No such file or directory)   Java action: // This file was generated by Mendix Studio Pro. // // 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 extendedworkbench.actions; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.webui.CustomJavaAction; import com.mendix.systemwideinterfaces.core.IMendixObject; import com.mendix.core.Core; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ByteArrayOutputStream; import java.util.List; import java.util.Optional; import java.util.Collections; import java.util.stream.Collectors; public class SaveDocumentsToFolder extends CustomJavaAction<java.lang.Void> { private java.util.List<IMendixObject> __DocumentsList; private java.util.List<system.proxies.FileDocument> DocumentsList; private java.lang.String Path; public SaveDocumentsToFolder(IContext context, java.util.List<IMendixObject> DocumentsList, java.lang.String Path) { super(context); this.__DocumentsList = DocumentsList; this.Path = Path; } @java.lang.Override public java.lang.Void executeAction() throws Exception { this.DocumentsList = java.util.Optional.ofNullable(this.__DocumentsList) .orElse(java.util.Collections.emptyList()) .stream() .map(__DocumentsListElement -> system.proxies.FileDocument.initialize(getContext(), __DocumentsListElement)) .collect(java.util.stream.Collectors.toList()); // BEGIN USER CODE for (system.proxies.FileDocument document : DocumentsList) { String fileName = document.getName(); byte[] fileContent = getFileContent(document.getMendixObject()); saveFile(Path, fileName, fileContent); } return null; // END USER CODE } /** * Returns a string representation of this action * @return a string representation of this action */ @java.lang.Override public java.lang.String toString() { return "SaveDocumentsToFolder"; } // BEGIN EXTRA CODE private byte[] getFileContent(IMendixObject fileDocument) throws IOException { try (InputStream inputStream = Core.getFileDocumentContent(getContext(), fileDocument); ByteArrayOutputStream buffer = new ByteArrayOutputStream()) { int nRead; byte[] data = new byte[1024]; while ((nRead = inputStream.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); return buffer.toByteArray(); } } private void saveFile(String folderPath, String fileName, byte[] content) throws IOException { File file = new File(folderPath + File.separator + fileName); try (FileOutputStream fos = new FileOutputStream(file)) { fos.write(content); } } // END EXTRA CODE }   Any idea on how to make it work?   Regards, Stefan  
asked
1 answers
1

Logically, the Mendix cloud does not have acces to your local file system, which is why the Java code works locally but not when running in the cloud. Typically these java actions are used in an on-prem deployment setup.

 

You could consider setting up a FTP server  if that fits your usecase.

answered