Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

SFTP simple Upload Java with ScriptMaster

Featured Replies

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

	    }

  • 8 months later...

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

			}

  • 4 months later...

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

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

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.