Get file from ftp server Java Action

0
  Hi,  Im am trying to have a scheduled event go to an FTP server and get an excel file and import. all of the import functionality is working I just cant get the file from the ftp server. Below you will find my code and the error it produces. Of course I understand that the error is refused to connect. I am certain the credentials are correct. My question is: Have I messed up the code somewhere? Or is there another approach to take? Or is it entirely that the server will not allow a connection for whatever reason.  As a side note, one of the passwords used has a # sign in it and when run the console always gives: Error: For input string: "everything before the # sign" and says Caused by: java.lang.NumberFormatException.  package importutility.actions; import java.io.*; import java.net.URL; import java.net.URLConnection; import org.apache.commons.io.FileUtils; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.poi.POIXMLDocument; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.mendix.core.Core; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.webui.CustomJavaAction; import jcifs.smb.*; import com.mendix.systemwideinterfaces.core.IMendixObject; public class ACT_SearchFileDirectory extends CustomJavaAction<IMendixObject> { private java.lang.String ImportDirectory; private java.lang.String FileExtension; private IMendixObject __XLSFileParameter1; private importutility.proxies.XLSFile XLSFileParameter1; public ACT_SearchFileDirectory(IContext context, java.lang.String ImportDirectory, java.lang.String FileExtension, IMendixObject XLSFileParameter1) { super(context); this.ImportDirectory = ImportDirectory; this.FileExtension = FileExtension; this.__XLSFileParameter1 = XLSFileParameter1; } @Override public IMendixObject executeAction() throws Exception { this.XLSFileParameter1 = __XLSFileParameter1 == null ? null : importutility.proxies.XLSFile.initialize(getContext(), __XLSFileParameter1); // BEGIN USER CODE ExcelFinder ExcelFinder = new ExcelFinder(); IContext context = this.getContext(); IMendixObject ScheduledFile = ExcelFinder.StartFinder(context, __XLSFileParameter1, ImportDirectory, FileExtension); return ScheduledFile; // END USER CODE } /** * Returns a string representation of this action */ @Override public java.lang.String toString() { return "ACT_SearchFileDirectory"; } // BEGIN EXTRA CODE private class ExcelFinder { public IMendixObject StartFinder(IContext context, IMendixObject XLSFile, String ImportDirectory, String FileExtension) { // Here the Strings for user, pass, and server are declared, excluded from this snippet for security try { URL url = new URL ("ftp://"+ user + ":" + pass + "@" + server); URLConnection urlc = url.openConnection(); InputStream is = urlc.getInputStream(); File downloadFile = File.createTempFile("temp", "temp"); downloadFile = new File(ImportDirectory + "." + FileExtension); is = new FileInputStream(downloadFile); OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile)); byte[] buffer = new byte[4096]; int len = 0; //Read length while ((len = is.read(buffer, 0, buffer.length)) != -1) { outputStream.write(buffer, 0, len); } outputStream.close(); is.close(); Core.storeFileDocumentContent(context, XLSFile, is); } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } return XLSFile; } } // END EXTRA CODE } Error: Connection refused: connect java.net.ConnectException: Connection refused: connect at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at sun.net.ftp.impl.FtpClient.doConnect(Unknown Source) at sun.net.ftp.impl.FtpClient.tryConnect(Unknown Source) at sun.net.ftp.impl.FtpClient.connect(Unknown Source) at sun.net.ftp.impl.FtpClient.connect(Unknown Source) at sun.net.www.protocol.ftp.FtpURLConnection.connect(Unknown Source) at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(Unknown Source) at importutility.actions.ACT_SearchFileDirectory$ExcelFinder.StartFinder(ACT_SearchFileDirectory.java:81) at importutility.actions.ACT_SearchFileDirectory.executeAction(ACT_SearchFileDirectory.java:52) at importutility.actions.ACT_SearchFileDirectory.executeAction(ACT_SearchFileDirectory.java:1) at com.mendix.systemwideinterfaces.core.UserAction.execute(UserAction.java:46) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.doCall(CoreActionHandlerImpl.scala:79) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.call(CoreActionHandlerImpl.scala:57) at com.mendix.core.actionmanagement.CoreAction.call(CoreAction.java:55) at com.mendix.basis.actionmanagement.ActionManager$1.execute(ActionManager.java:186) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:32) at com.mendix.basis.actionmanagement.ActionManager.executeSync(ActionManager.java:191) at com.mendix.basis.component.InternalCore.execute(InternalCore.java:625) at com.mendix.modules.microflowengine.actions.actioncall.JavaAction.execute(JavaAction.scala:64) at com.mendix.modules.microflowengine.microflow.impl.MicroflowObject.execute(MicroflowObject.java:47) at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAfterBreakingIfNecessary(MicroflowImpl.java:201) at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAction(MicroflowImpl.java:157) at com.mendix.systemwideinterfaces.core.UserAction.execute(UserAction.java:46) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.doCall(CoreActionHandlerImpl.scala:79) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.call(CoreActionHandlerImpl.scala:57) at com.mendix.core.actionmanagement.CoreAction.call(CoreAction.java:55) at com.mendix.basis.actionmanagement.ActionManager$1.execute(ActionManager.java:186) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:32) at com.mendix.basis.actionmanagement.ActionManager.executeSync(ActionManager.java:191) at com.mendix.basis.component.InternalCore.executeSync(InternalCore.java:703) at com.mendix.modules.microflowengine.actions.SubMicroflowAction.execute(SubMicroflowAction.scala:51) at com.mendix.modules.microflowengine.microflow.impl.MicroflowObject.execute(MicroflowObject.java:47) at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAfterBreakingIfNecessary(MicroflowImpl.java:201) at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAction(MicroflowImpl.java:157) at com.mendix.systemwideinterfaces.core.UserAction.execute(UserAction.java:46) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.doCall(CoreActionHandlerImpl.scala:79) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.call(CoreActionHandlerImpl.scala:57) at com.mendix.core.actionmanagement.CoreAction.call(CoreAction.java:55) at com.mendix.basis.actionmanagement.ActionManager$1.execute(ActionManager.java:186) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:32) at com.mendix.basis.actionmanagement.ActionManager.executeSync(ActionManager.java:191) at com.mendix.basis.component.InternalCore.executeSync(InternalCore.java:703) at com.mendix.modules.microflowengine.actions.other.NestedLoopedMicroflowAction.$anonfun$executeLoop$2(NestedLoopMicroflowAction.scala:70) at com.mendix.modules.microflowengine.actions.other.NestedLoopedMicroflowAction.$anonfun$executeLoop$2$adapted(NestedLoopMicroflowAction.scala:61) at scala.collection.TraversableLike$WithFilter.$anonfun$foreach$1(TraversableLike.scala:789) at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:59) at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:52) at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48) at scala.collection.TraversableLike$WithFilter.foreach(TraversableLike.scala:788) at com.mendix.modules.microflowengine.actions.other.NestedLoopedMicroflowAction.executeLoop(NestedLoopMicroflowAction.scala:61) at com.mendix.modules.microflowengine.actions.other.NestedLoopedMicroflowAction.execute(NestedLoopMicroflowAction.scala:43) at com.mendix.modules.microflowengine.microflow.impl.MicroflowObject.execute(MicroflowObject.java:47) at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAfterBreakingIfNecessary(MicroflowImpl.java:201) at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAction(MicroflowImpl.java:157) at com.mendix.systemwideinterfaces.core.UserAction.execute(UserAction.java:46) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.doCall(CoreActionHandlerImpl.scala:79) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.call(CoreActionHandlerImpl.scala:57) at com.mendix.core.actionmanagement.CoreAction.call(CoreAction.java:55) at com.mendix.basis.actionmanagement.ActionManager$1.execute(ActionManager.java:186) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:32) at com.mendix.basis.actionmanagement.ActionManager.executeSync(ActionManager.java:191) at com.mendix.basis.component.InternalCore.execute(InternalCore.java:625) at com.mendix.basis.actionmanagement.UserScheduledAction.executeScheduledAction(ScheduledAction.scala:73) at com.mendix.basis.actionmanagement.ScheduledAction$ScheduledRun.$anonfun$run$1(ScheduledAction.scala:141) at com.mendix.basis.actionmanagement.IMonitoredAction$$anon$1.execute(IMonitoredAction.scala:47) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:32) at com.mendix.basis.actionmanagement.IMonitoredAction.monitor(IMonitoredAction.scala:49) at com.mendix.basis.actionmanagement.IMonitoredAction.monitor$(IMonitoredAction.scala:25) at com.mendix.basis.actionmanagement.ScheduledAction$ScheduledRun.monitor(ScheduledAction.scala:132) at com.mendix.basis.actionmanagement.ScheduledAction$ScheduledRun.run(ScheduledAction.scala:141) at com.mendix.basis.actionmanagement.ScheduledAction.execute(ScheduledAction.scala:130) at com.mendix.basis.actionmanagement.ScheduledAction.run(ScheduledAction.scala:111) at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at java.util.concurrent.FutureTask.runAndReset(Unknown Source) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Thanks, Benjamin Griggs
asked
1 answers
1

Instead of writing Java code to access an FTP server, you should use the sFTPs module from the AppStore. This is a module which has been verified to work, which saves you development time.

answered