September 29, 201114 yr Same comments as the upload basically. There's some FTP code posted but here's a basic SFTP version. Feel free to correct me or add to it and post. Some elements actually might not be needed or could be better written. I'm not a programmer per se so be nice..... /********************************************************************************************************** * SFTP Download * parameters sftp_hostname, sftp_username, sftp_password, sftp_port (should be 22 I believe) * remote_directorypath, remote_filename, local_directorypath, local_filename * * JSch is licensed under BSD style license. * http://www.jcraft.com/jsch * http://www.jcraft.com/jsch/examples/ * http://epaul.github.com/jsch-documentation/javadoc/ * * Jar file: http://sourceforge.net/projects/jsch/files/jsch.jar/0.1.44/jsch-0.1.44.jar/download * * Disclaimer: This code is provided as is without any guarantees,warranties or representations. * User is responsible to verify all legalities * Finally - This is just a base. Feel free to add to it and post..... **********************************************************************************************/ import com.jcraft.jsch.*; import java.io.*; JSch jsch = new JSch(); Session session = null; String result_text = ""; // Use External Function SMGetVariable( variableName ) to return to FM try { session = jsch.getSession(sftp_username, sftp_hostname, Integer.parseInt(sftp_port)); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(sftp_password); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; // Local folders with spaces, like Program Files, causes problems but I'm sure // someone with better coding skills can easily fix this I'm sure sftpChannel.get(remote_directorypath + remote_filename, local_directorypath + local_filename); sftpChannel.exit(); session.disconnect(); // check for file on local drive if (new File(local_directorypath + local_filename).exists()) { result_text = result_text + "Download Complete"; // returns to FM } else { result_text = result_text + "Download Failed"; // returns to FM } } catch (JSchException e) { result_text = result_text + e.printStackTrace(); // returns error to FM } catch (SftpException e) { result_text = result_text + e.printStackTrace(); // returns error to FM }
Create an account or sign in to comment