September 29, 201114 yr 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 Upload * parameters are * sftp_hostname, sftp_username, sftp_password, sftp_port (should be 22 I believe) * remote_directorypath, 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 = (ChannelSftp)channel; channelSftp.cd(remote_directorypath); // check for file on local drive if (new File(local_directorypath + local_filename).exists()) { File f = new File(local_directorypath + local_filename); channelSftp.put(new FileInputStream(f), f.getName()); // check for file after upload using jsch's ... Display info about path cmd Thread.sleep(2000); if(!channelSftp.stat(remote_directorypath + "/" + local_filename)){ result_text = result_text + "Upload Failed"; // returns to FM } else { result_text = result_text + "Upload Completed"; // returns to FM } } else { result_text = result_text + "File not found on local drive. Verify path and file name"; // 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 }
June 7, 201213 yr There's a minor bug with that code when running under Windows. Once the file is uploaded it's left in a locked state. If you're running a process involving an export and upload you'll then get an error 800 from FileMaker as it can't overwrite the file. Here's a slightly revised version that corrects the issue and works correctly cross platform. On line 40 it creates a FileInputStream that is explicitly closed once the upload is done. /********************************************************************************************************** * SFTP Upload * parameters are * sftp_hostname, sftp_username, sftp_password, sftp_port (should be 22 I believe) * remote_directorypath, 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 = (ChannelSftp)channel; channelSftp.cd(remote_directorypath); // check for file on local drive if (new File(local_directorypath + local_filename).exists()) { File f = new File(local_directorypath + local_filename); FileInputStream ioStream=new FileInputStream(f); channelSftp.put(ioStream, f.getName()); // check for file after upload using jsch's ... Display info about path cmd Thread.sleep(2000); ioStream.close(); if(!channelSftp.stat(remote_directorypath + "/" + local_filename)){ result_text = result_text + "Upload Failed"; // returns to FM } else { result_text = result_text + "Upload Completed"; // returns to FM } } else { result_text = result_text + "File not found on local drive. Verify path and file name"; // 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 }
October 21, 201213 yr I have tried to use this but after a long wait I get the result 'null' and no file transfer occurs. I have local directory paths in the formats /Users/username/Desktop and /Users/username/Desktop/ and similar formats for the remote directory path. Anyone give me any pointers, the hostname, port, username and password are all correct Thanks Tim
October 25, 201213 yr Tim, It's not a certainty, but the problem is most likely caused by the server being set for key authentication and NOT password authentication. Can you connect to it with Filezilla or other sftp client software? You may need to add key authentication to the code provided. In you client software, are keys set (e.g. in Filezilla, Settings|SFTP|Private keys)?
Create an account or sign in to comment