We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Pages: 1 2 
FTP Upload (Read 12094 times)
FTP Upload
Nov 5th, 2005, 2:03am
 
Is it possible to create a sort of ftp file upload program in processing?

How might you go about opening a dialog to have a user browse their computer for a file?

How might processing connect and upload to an ftp server?

I don't think I've seen either of these done, how might I start going about it?
Re: FTP Upload
Reply #1 - Nov 5th, 2005, 1:57pm
 
You can find some links to open source FTP libraries for Java here: http://del.icio.us/garuda/ftp

Personally I have used edtFTPj with success, it's not very complicated and seems to do the job. Just make sure you capture Exceptions appropriately, it's generally a bad idea to send a few hundred connection attempts per second...

This is the URL for edtFTPj:
http://www.enterprisedt.com/products/edtftpj/overview.html
Re: FTP Upload
Reply #2 - Mar 13th, 2006, 10:51pm
 
Hey guys

What happened? Did you get it to work?

How about a code sample and some easy to follow library type instructions?

Cheers Andy
Re: FTP Upload
Reply #3 - Mar 20th, 2006, 11:18am
 
Thanks for the heads up Marius!

I've just set this up and have a source file that I'll post when I get home that demonstrates the usage.

Here goes:

Make sure you download the code as references above. pop the .jar into your directory and then do this to test:

All logs are printed to the Procssing monitor.

Any probs just post here!

Good luck.
Code:



FTPClient ftp;

void setup () {

try {
// set up client
ftp = new FTPClient();
ftp.setRemoteHost("YourFtp"); // ie. ftp.site.com
FTPMessageCollector listener = new FTPMessageCollector();
ftp.setMessageListener(listener);

// connect
println ("Connecting");
ftp.connect();

// login
println ("Logging in");
ftp.login("yourUsername", "yourPassword");

// set up passive ASCII transfers
println ("Setting up passive, ASCII transfers");
ftp.setConnectMode(FTPConnectMode.PASV);
ftp.setType(FTPTransferType.ASCII);

// get directory and print it to console
println ("Directory before put:");

String[] files = ftp.dir(".", true);
for (int i = 0; i < files.length; i++)
println (files[i]);

// copy file to server
println ("Putting file");
ftp.put("/localPath/test.txt", "/remotePath/test.txt");

// get directory and print it to console
println ("Directory after put");

files = ftp.dir(".", true);
for (int i = 0; i < files.length; i++)
println (files[i]);

// copy file from server

println ("Getting file");

ftp.get("/localPath/test.txt" + ".copy", "/remotePath/test.txt");

// delete file from server
println ("Deleting file");
ftp.delete("/remotePath/test.txt");


// get directory and print it to console
println ("Directory after delete");
files = ftp.dir("", true);
for (int i = 0; i < files.length; i++)
println (files[i]);

// Shut down client
println ("Quitting client");
ftp.quit();

String messages = listener.getLog();
println ("Listener log:");

println(messages);
println ("Test complete");

} catch (Exception e) {

println ("Demo failed");

}

}



Bob's your uncle!
Re: FTP Upload
Reply #4 - Mar 20th, 2006, 5:02pm
 
Excellent, Mark. I was meaning to write this up, but didn't get around to it. If you posted this tip on processinghacks.com too I'm sure everybody would be eternally grateful...
Re: FTP Upload
Reply #5 - Mar 21st, 2006, 9:17am
 
Sure thing!

I'll get around to it in the next day or two.


Seems to be working nicely off of threads too! So you don't have to hang the cpu when transfering files.

Thanks to JohnG for having posted an threadedImageLoader elsewhere on the forum for giving me the idea!
Re: FTP Upload
Reply #6 - Mar 21st, 2006, 5:30pm
 
It's definitely a good idea to use threads for comm stuff like that. Just implement a proper way of keeping track of what tasks have been completed etc.

Proper exception-handling with timeouts and delay-before-retry is also good, you might want to mention that. I got banned from my own web hosting server for trying to access my own site a few thousand times a second. I believe they call that a denial-of-service attack...
Re: FTP Upload
Reply #7 - Mar 21st, 2006, 5:51pm
 
I've got a simple local and remote index system in place that just synchronizes the folders, so if you walk out of range or lose the connection, once it's re-established uploading/ downloading recommences.

I had guessed a bit about hammering the server, and so have a timeout after 10 errors, to leave things off for a while before retrying.

Does anyone know what the standards are for ftp in terms of no of tries, timeouts etc., so that one can be sure to conform and avoid your misfortune.
Re: FTP Upload
Reply #8 - Mar 29th, 2006, 11:56am
 
Hi

This works great, I've added some notes and some more methods so it's a bit easier to follow (perhaps!)

Do this:

Download edtftpj-1.5.2.zip from http://www.enterprisedt.com/products/edtftpj/download/edtftpj-1.5.2.zip

Create a "code" folder in the Sketch Folder

Unzip and copy the file edtftpj-1.5.2.jar into the code folder

Run this (needs editing for web site login):

// Declare a new FTPClient
FTPClient ftp;

// Declare an array to hold directory listings
String[] files;

void setup ()
{

 try
 {

   // set up a new ftp client
   ftp = new FTPClient();
   ftp.setRemoteHost("www.whatever.webhosting.com"); // ie. ftp.site.com

   // set up listener
   FTPMessageCollector listener = new FTPMessageCollector();
   ftp.setMessageListener(listener);

   // connect to the ftp client
   println ("Connecting");
   ftp.connect();

   // login to the ftp client
   println ("Logging in");
   ftp.login("whateverAdmin", "whateverPassword");

   // set up in passive mode
   println ("Setting up passive, ASCII transfers");
   ftp.setConnectMode(FTPConnectMode.PASV);

   // set up for ASCII transfers
   ftp.setType(FTPTransferType.ASCII);

   // get the directory contents and print it to console
   println ("Directory before put:");
   files = ftp.dir(".", true);
   for (int i = 0; i < files.length; i++)
   {
     println (i+" "+files[i]);
   }

   // copy ASCII file to server and overwrite the existing file
   println ("Putting file");
   ftp.put("C:\\Desktop\\test.txt", "test.txt", false);

   // get directory and print it to console
   println ("Directory after put");
   files = ftp.dir(".", true);
   for (int i = 0; i < files.length; i++)
   {
     println (i+" "+files[i]);
   }

   // copy ASCII file from server
   println ("Getting file");
   ftp.get("C:\\Desktop\\test.txt" + ".copy", "test.txt");

   // delete file from server
   println ("Deleting file");
   ftp.delete("test.txt");

   // get directory and print it to console
   println ("Directory after delete");
   files = ftp.dir("", false);
   for (int i = 0; i < files.length; i++)
   {
     println (i+" "+files[i]);
   }

   // change directory
   println("Moving up a directory");
   ftp.cdup();

   // change directory in a different way
   println("Moving up a directory");
   ftp.cdup();
   
   // change directory in a different way
   println("Moving up a directory");
   ftp.chdir("..");

   // get directory and print it to console
   println ("Directory listing after move");
   files = ftp.dir("", true);
   for (int i = 0; i < files.length; i++)
   {
     println (i+" "+files[i]);
   }

   println("Moving into a directory");
   ftp.chdir(files[2].substring(55));

   try
   {
     // try to make a directory
     println("Make a new directory");
     ftp.mkdir("tempDir");
   }
   catch (Exception e)
   {
     //Print out the type of error
     println("Error "+e);
   }  

   //println("Delete a directory");
   //ftp.rmdir("tempDir");

   println("Moving into a directory");
   ftp.chdir("tempDir");
   
   // change directory in a different way
   println("The current directory");
   println(ftp.pwd());

   // get a directory listing and print it to console
   println ("Directory listing after making a new directory");
   files = ftp.dir("", true);
   for (int i = 0; i < files.length; i++)
   {
     println (i+" "+files[i]);
   }

   // set up for ASCII transfers
   ftp.setType(FTPTransferType.BINARY);

   // copy BINARY file to server  and overwrite the existing file
   println ("Putting file");
   ftp.put("C:\\Desktop\\bath-time.jpg", "bath-time.jpg", false);

   // run a raw FTP command on the server
   try
   {
     println("Run a FTP command");
     // add some commands if required
     String[] ftpParams = { "" };
     // send the FTP command followed by params
     ftp.quote( "SITE CHMOD 755 /web/tempDir/bath-time.jpg", ftpParams);
   }
   catch (Exception e)
   {

     //Print out the type of error
     println("Message "+e);

   }

   // Shut down client
   println ("Quitting client");
   ftp.quit();

   // Print out the listener messages
   String messages = listener.getLog();
   println ("Listener log:");

   // End message - if you get to here it must have worked
   println(messages);
   println ("Test complete");

 }
 catch (Exception e)
 {

   //Print out the type of error
   println("Error "+e);

 }

}
Re: FTP Upload
Reply #9 - Apr 4th, 2008, 1:10am
 
Hi,

I was experimenting this ftp code on my pc. It works but the image has to be really small. like 4.00 KB.
Is it normal?
I get this error message after a minute or two of trying 'putting file':

Error java.net.SocketException: Connection reset by peer: socket write error

The file appears on the server when I look with filezilla but is 0kb.

If you have any ideas or other solutions. I found this from the alpha board. It seems to be a solution that fjen came up with but the link is broken.
http://processing.org/discourse/yabb/YaBB.cgi?board=Integrate;action=display;num=1106908696
(reply#2 from fjen).

I don't really want to faff around with php as I don't know much about it and knowing how to do this with processing could bring me to more experimentations.


Re: FTP Upload
Reply #10 - Apr 4th, 2008, 11:51am
 
hello again.
'la nuit m'a porte conseil' obviously.

My firewall ( kerio ) was somehow blocking big files transfers. This firewall was also blocking me from accessing my email and blocking windows update.
I now use the windows' build in firewall and it works great!!

Thanks for finding a way to use ftp transfers on processing.
Is there a way to monitor the advancement of the upload?

 
Re: FTP Upload
Reply #11 - Mar 23rd, 2009, 10:55pm
 
thanks! work great
Re: FTP Upload
Reply #12 - Apr 28th, 2009, 12:06pm
 
Thanks. This is great.
Re: FTP Upload
Reply #13 - May 29th, 2009, 7:50am
 
This works great for me when I use on local machine. Thank you!
When a user places an order their choices are sent to the orders directory on my server.
I was very disappointed when I uploaded the application to the server, however.
I get errors like this:
"java.lang.NoClassDefFoundError: com/enterprisedt/net/ftp/FTPMessageListener"
After a little research I got the impression that its something to do with using extra libraries in an exported application. I'm not sure how to deal with this any help would really be appreciated!
I cant post code as this is my first post
Re: FTP Upload
Reply #14 - May 31st, 2009, 2:47pm
 
I dunno if this helps anyone but I have been using the follwing method for FTP in processing:

1) Create ftp batch file with printwriter
2) run it with open()
3) delete all text in the batch file OR have a script that deletes the batchfile(to remove sensitive username/password info)

it's a quick kludge that has been working for me, hope it helps someone out there.
Pages: 1 2