Click to See Complete Forum and Search --> : FTP in console app
rockies1
Oct 10th, 2002, 07:55 AM
Anyone know where I can find some examples of how to FTP (pull from a server only, no uploading) a file via java in a console app?
Thanks!
Morgan
billrogers
Oct 10th, 2002, 12:16 PM
This is not hard at all. Check out sun's ftp client. It is not documented by sun at all by you can find some info on it at the following:
http://www.informatik.tu-freiberg.de/~polet/docs/sun.net.TransferProtocolClient.html
http://www.aoindustries.com/docs/aocode-public/com/aoindustries/net/FtpClient.html
here is an example that shows how to upload and download.
import sun.net.*;
import sun.net.ftp.*;
import java.io.*;
public class ftp_utility {
public ftp_utility () {
FtpClient fcMyFtp = new FtpClient();
try {
int ch;
fcMyFtp.openServer("myftpsite");
fcMyFtp.login("myname","mypassword");
TelnetInputStream tisList =
fcMyFtp.list();
while( (ch = tisList.read()) != -1 )
System.out.print((char)ch);
// ... other stuff.
fcMyFtp.cd("temp");
fcMyFtp.binary();
// writing a file:
FileInputStream fis = new
FileInputStream( "/home/gadio/temp/tempfile" );
TelnetOutputStream tos =
fcMyFtp.put("tempfile.new");
byte buffer[] = new byte[1000];
int len;
while( (len = fis.read(buffer)) != -1 ) {
tos.write(buffer,0,len);
}
fis.close();
tos.close();
// reading a file:
FileOutputStream fos = new
FileOutputStream( "/home/gadio/temp/tempfi
le.read" );
TelnetInputStream tis =
fcMyFtp.get("tempfile.new");
while( (len = tis.read(buffer)) != -1 ) {
fos.write(buffer,0,len);
}
tis.close();
fos.close();
} catch( IOException e ) {
e.printStackTrace();
}
}
public static void main(String args[]) {
ftp_utility f = new ftp_utility();
}
}
billrogers
Oct 10th, 2002, 12:40 PM
another thing you could do is execute a runtime to use say the microsoft ftp client, but you will lose platform independence doing this which may not be bad if you know what platform you are always going to run on.
rockies1
Oct 10th, 2002, 01:35 PM
That worked beautifully, thank you!
billrogers
Oct 10th, 2002, 02:24 PM
your welcome, glad to be of help
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.