/** * Default constructor. Create a new FTP client and connect to a specified server. * * @param server Address of an FTP server */ public Ftp(String server) { serverAddr = server; debug = true; try { sock = new Socket(server, 21); System.out.println("Connected to " + server); out = new PrintWriter(sock.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(sock.getInputStream())); System.out.println(getResponse()); } catch (IOException e) { System.err.println(e.getMessage()); System.exit(1); } }
/** * Main method which is in charge of parsing and interpreting user input and interacting with the * FTP client. * * @param args Command line arguments; should only contain the server */ public static void main(String args[]) { boolean eof = false; String input = null; Scanner in = new Scanner(System.in); if (args.length != 1) { System.err.println("Usage: java Ftp <server>"); System.exit(1); } /* Create a new FTP client */ Ftp client = new Ftp(args[0]); do { try { System.out.print(prompt); input = in.nextLine(); } catch (NoSuchElementException e) { eof = true; } /* Keep going if we have not hit end of file */ if (!eof && input.length() > 0) { int cmd = -1; String argv[] = input.split("\\s+"); for (int i = 0; i < commands.length && cmd == -1; i++) if (commands[i].equalsIgnoreCase(argv[0])) cmd = i; /* Execute the command */ switch (cmd) { case ASCII: client.ascii(); break; case BINARY: client.binary(); break; case CD: client.cd(argv[1]); break; case CDUP: client.cdup(); break; case DEBUG: client.toggleDebug(); break; case DIR: client.dir(); break; case GET: client.get(argv[1]); break; case HELP: for (int i = 0; i < HELP_MESSAGE.length; i++) System.out.println(HELP_MESSAGE[i]); break; case PASSIVE: client.pasv(); break; case PWD: client.pwd(); break; case QUIT: eof = true; break; case USER: client.user(argv[1]); String password = PasswordField.getPassword("Password: "******"Invalid command"); } } } while (!eof); /* Clean up */ client.close(); }