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
Printable View
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
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...colClient.html
http://www.aoindustries.com/docs/aoc...FtpClient.html
here is an example that shows how to upload and download.
Code: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();
}
}
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.
That worked beautifully, thank you!
your welcome, glad to be of help