import java.io.*; import sun.net.ftp.FtpClient; public class Download { public static void main(String[] args) { FtpClient fc; String filename = ""; String hostname = ""; String username = "anonymous"; String password = ""; String directory = ""; try { int lastSlash = args[1].lastIndexOf('/'); filename = args[1].substring(lastSlash+1); directory = args[1].substring(0,lastSlash); hostname = args[0]; } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Usage: java Download host path/file username password"); System.exit(1); } try { username = args[2]; } catch (ArrayIndexOutOfBoundsException e) { username = "anonymous"; } try { password = args[3]; } catch (ArrayIndexOutOfBoundsException e) { password = username + "@"; } try { fc = new FtpClient(hostname); fc.login(username, password); fc.binary(); fc.cd(directory); InputStream theFile = fc.get(filename); int i = 0; FileOutputStream fos = new FileOutputStream(filename); while ((i = theFile.read()) != -1) { fos.write((byte) i); } fc.closeServer(); fos.close(); } catch (IOException e) { System.err.println(e); } } }