1   /*
2    * Copyright 2005 jMethods, Inc. All rights reserved.
3    * jMethods PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4    */
5   
6   package com.myjavaworld.ftp.examples;
7   
8   import java.io.File;
9   
10  import com.myjavaworld.ftp.ConnectionException;
11  import com.myjavaworld.ftp.FTPClient;
12  import com.myjavaworld.ftp.FTPConstants;
13  import com.myjavaworld.ftp.FTPException;
14  import com.myjavaworld.ftp.ListParser;
15  import com.myjavaworld.ftp.RemoteFile;
16  
17  /**
18   * This example demonstrates how to connect to an FTP site and download a file
19   * to the local system.
20   * 
21   * @author Sai Pullabhotla, psai [at] jMethods [dot] com
22   * @version 1.0
23   */
24  public class Download {
25  
26      /**
27       * @param args
28       *            command line arguments.
29       * @throws InstantiationException
30       *             propogated
31       * @throws ClassNotFoundException
32       *             propogated
33       * @throws IllegalAccessException
34       *             propogated
35       * @throws FTPException
36       *             propogated
37       * @throws ConnectionException
38       *             propogated
39       */
40  
41      public static void main(String[] args) throws InstantiationException,
42          ClassNotFoundException, IllegalAccessException, FTPException,
43          ConnectionException {
44  
45          //Determine the FTPClient implementation that you would like to use.
46          //For most FTP servers, the DefaultFTPClient should work.
47          String className = "com.myjavaworld.ftp.DefaultFTPClient";
48  
49          //Use reflection to find the class and obtain an instance of the class.
50          FTPClient client = (FTPClient) Class.forName(className).newInstance();
51  
52          //Determine the directory list parser that we would like to use in this
53          //session. For most FTP servers, the default implementatiom should work
54          //fine.
55          ListParser parser = (ListParser) Class.forName(
56              "com.myjavaworld.ftp.DefaultListParser").newInstance();
57  
58          //Set the list parser to use with the FTP client.
59          client.setListParser(parser);
60  
61          //Connect to the FTP server.
62          System.out.println("Connecting...");
63          client.connect("ftp.netscape.com");
64          System.out.println("Connected. ");
65  
66          //Login to the FTP server.
67          System.out.println("Logging in...");
68          client.login("anonymous", "you@yourcompany.com");
69          System.out.println("Logged in. ");
70  
71          //Download a file
72          RemoteFile source = parser.createRemoteFile(
73              "/pub/netscape7/english/7.1/windows/win32/NSSetup.exe", false);
74          File destination = new File("/temp/NSSetup.exe");
75          System.out.println("Downloading: " + source);
76          client.download(source, destination, FTPConstants.TYPE_BINARY, false);
77          System.out.println("Done. ");
78  
79          //Disconnect from the FTP server.
80          System.out.println("Disconnecting...");
81          client.disconnect();
82          System.out.println("Disconnected. ");
83      }
84  }