Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 4429 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

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

	    }

  • Like 1
  • 8 months later...
Posted

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...
Posted

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

Posted

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)?

This topic is 4429 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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