Jump to content

FTP Upload with Scriptmaster!


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

Recommended Posts

Requirements:

ScriptMaster for FMP (AWESOME!)

Download simpleftp.jar (and put it somewhere you can find it later, like the Groovy lib folder if you have Groovy installed ;) )

Should be in a startup script: (should only be loaded once per FM session, dunno if loading more times will break it or not.)


SMLoadJar ( "file://C:/Program Files/Groovy/lib/simpleftp.jar" )





Example code: (shamelessly ripped from the SimpleFTP website)



EvaluateGroovy("

	

	import org.jibble.simpleftp.*;

	

	def ftp = new SimpleFTP(); //modified this line

	    

	// Connect to an FTP server on port 21.

	ftp.connect("ftp.somewhere.net", 21, "username", "password");

	

	// Set binary mode.

	ftp.bin();

	

	// Change to a new working directory on the FTP server.

	//ftp.cwd("web");

	

	// Upload some files.

	ftp.stor(new File("file://C:/Program Files/Groovy/lib/simpleftp.jar"));

	//ftp.stor(new File("comicbot-latest.png"));

	

	// You can also upload from an InputStream, e.g.

	// Well... I haven't tested this in ScriptMaster and I cannot think

	// of a reason to use it from Filemaker... But, here is is anyway.

	//ftp.stor(new FileInputStream(new File("test.png")), "test.png");

	//ftp.stor(someSocket.getInputStream(), "blah.dat");

	

	// Quit from the FTP server.

	ftp.disconnect();

	

")

Edited by Guest
Link to comment
Share on other sites

  • 1 year later...
  • 9 months later...
  • Newbies

Hello there,

I'm using the Jakarta Commons Net-Library from apache.org for FTP.

It works very nice under FM Pro 10 (Win7).

Don't know about different versions or platforms.

To use it, just download the Library and add commons-net-ftp-2.0.jar to the List of Jar-Files and connect like this:


ftp = new FTPClient();

ftp.connect( "ftp.yourhost.com" );

ftp.login( "username", "password" );

You can get the download, further details and examples, as well as a complete javadoc on the following site:

http://commons.apache.org/net/

This library is able to deal with several other protocols, e.g. POP3 and SMTP. I haven't tested these features yet though.

Regards,

Lumpi23

Link to comment
Share on other sites

I've been using this as well for some time.

where FM parameters ftp_hostname, ftp_username, ftp_password are the hostname, username, and password. Local filename is local path e.g. C:text.dat, remote_directorypath is e.g. /httpdocs, remote_filename is the remote filename, and mode_bin_or_text is the mode, bin or text for binary or text. I also set two timeouts to 20 seconds so it won't hang (20,000 msec) per below.

Presumably, you could also run a java program from FM, by running javac followed by java (no SM plugin required), but the plugin certainly simplifies passing parameters and retrieving results from the JVM.

One thing I would like to do with this plugin, is figure out how to use jniwrapper, winapi, etc to get a handle on, and attach to, a webviewer control to permit it to be automated.

// NOTE: This uses http://commons.apache.org/net/

// FAQ for commons here: http://wiki.apache.org/jakarta-commons/Net/FrequentlyAskedQuestions



import org.apache.commons.net.ftp.FTPClient;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.PrintWriter;

import org.apache.commons.net.ftp.FTP;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPConnectionClosedException;

import org.apache.commons.net.ftp.FTPReply;



try {

FTPClient connection = new FTPClient();

connection.connect(ftp_hostname);            

boolean logged = connection.login(ftp_username, ftp_password);    

if (logged) 



{

connection.setSoTimeout(20000);

connection.setDataTimeout(20000);



if (mode_bin_or_text=="bin")

{

  connection.setFileType(connection.BINARY_FILE_TYPE);

}



if (mode_bin_or_text=="text")

{

  connection.setFileType(connection.ASCII_FILE_TYPE);

}





    connection.changeWorkingDirectory(remote_directorypath);

connection.enterLocalPassiveMode();

           InputStream input;



            input = new FileInputStream(local_filename);



               connection.storeFile(remote_filename, input);



                input.close();



    connection.logout();





connection.disconnect();



return"Upload Succeeded";

}

else

return"Error while connecting to FTP...............";

} catch (Exception e) {

return e;

}

Link to comment
Share on other sites

  • 2 months later...

I'm trying this code but it doesn't work.

After loaded jar files, I've registered this function:

RegisterGroovy( "FTPSendFile ( ftp_hostname; ftp_username ; ftp_password ; local_filename ; remote_directorypath ; remote_filename ; mode_bin_or_text )"; "

FTPClient connection = new FTPClient();

connection.connect(ftp_hostname);            

boolean logged = connection.login(ftp_username, ftp_password);



if (logged)

{

connection.setSoTimeout(20000);

connection.setDataTimeout(20000);



if (mode_bin_or_text=='bin') { connection.setFileType(connection.BINARY_FILE_TYPE); }

if (mode_bin_or_text=='text') { connection.setFileType(connection.ASCII_FILE_TYPE); }



connection.changeWorkingDirectory(remote_directorypath);

connection.enterLocalPassiveMode();



InputStream input;

input = new FileInputStream(local_filename);

connection.storeFile(remote_filename, input);

input.close();

connection.logout();

connection.disconnect();



// Succeded

return '1';

} else return 'ERROR';

" )




And then called like:


FTPSendFile( "";"testuser";"testpass"; $$MyFile ; "/" ; "example.pdf" ; "bin" )

It returns a blank value, and nothing arrives to the server.

Any help?

Link to comment
Share on other sites

Added import classes at the beginning of the script:

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.PrintWriter;

import org.apache.commons.net.ftp.FTP;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPConnectionClosedException;

import org.apache.commons.net.ftp.FTPReply;

And now returns a '?' value.

Any help?

Link to comment
Share on other sites

Finally worked!


RegisterGroovy( "FTPSendFile ( ftp_hostname ; ftp_username ; ftp_password ; local_filename ; remote_directorypath ; remote_filename )"; "



import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import org.apache.commons.net.ftp.*;



try {

FTPClient connection = new FTPClient();

connection.connect(ftp_hostname);            

boolean logged = connection.login(ftp_username, ftp_password);

if (!logged) return "0";



connection.setSoTimeout(20000);

connection.setDataTimeout(20000);

connection.setFileType(connection.BINARY_FILE_TYPE);



connection.changeWorkingDirectory(remote_directorypath);

connection.enterLocalPassiveMode();



InputStream input;

input = new FileInputStream(local_filename);

connection.storeFile(remote_filename, input);

input.close();



return "1";

} catch (Exception e) {

return e;

}

" )

Now I'll try to FTPGetFile...

:

Link to comment
Share on other sites

  • 1 month later...

FTP get.. doesn't check if the file is actually there but seems to work


// FTPget ( ftp_host ; ftp_user ; ftp_password ; remote_path ; local_path ; fm_file ; mode_bin_or_text )

// 11_01_20 JR

// v1

// downloads file from fTP site, returns true if successful or false or error



import org.apache.commons.net.ftp.*



_sep = System.getProperty("file.separator")

try {

	conn = new FTPClient()

	conn.connect(ftp_host)

	boolean logged = conn.login(ftp_user, ftp_password)

	if(logged) {

		conn.setSoTimeout(20000)

		conn.setDataTimeout(20000)

		conn.changeWorkingDirectory(remote_path)

		conn.enterLocalPassiveMode()

		fos = new FileOutputStream(local_path + _sep + fm_file)

		conn.retrieveFile(fm_file, fos)

		fos.close()

		conn.logout()

		conn.disconnect()

		//return"Download Succeeded.."

		return true

	} else {

		//return"Error while connecting to FTP..............."

		return false

	} // end if

}

catch(Exception e) {

	return e

}

Link to comment
Share on other sites

  • 2 years later...

This topic is 3804 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.