import sun.net.ftp.*;
import sun.net.*;
import java.io.*;
public class FTP {
public static void main(String args[]) throws Exception {
// Conatct server and log in
String server = "192.168.1.4";
String user = "joe";
String passwd = "bloggs";
FtpClient client = new FtpClient();
System.err.println("Opening server ...");
client.openServer(server);
System.err.println("Logging in...");
client.login(user, passwd);
System.err.println("Changing to binary ...");
client.binary();//
// change dir
//client.cd("transfer");
// pwd
String pwd = client.pwd();
System.err.println("cwd : " +pwd);
TelnetInputStream in;
BufferedOutputStream bos;
int buffer = 0;
// list dir
System.err.println("Listing dir ...");
in = client.list();
bos = new BufferedOutputStream(System.out);
while ((buffer = in.read()) != -1) {
bos.write(buffer);
}
bos.flush();
bos.close();
in.close();
// Get a file
System.err.println("Getting file ...");
in = client.get("gddemo_jpg.c");
bos = new BufferedOutputStream(new FileOutputStream("C:/ftp_downloaded.txt"));
buffer = 0;
while ((buffer = in.read()) != -1) {
bos.write(buffer);
}
bos.flush();
bos.close();
in.close();
// Send a file
TelnetOutputStream out = client.put("floodmap.png");
File file = new File("C:/floodmap.png");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] b = new byte[(int)file.length()];
bis.read(b);
out.write(b);
out.flush();
out.close();
bis.close();
client.closeServer();
}
}