View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package examples;
19  
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.io.PrintWriter;
26  
27  import org.apache.commons.net.PrintCommandListener;
28  import org.apache.commons.net.ftp.FTP;
29  import org.apache.commons.net.ftp.FTPClient;
30  import org.apache.commons.net.ftp.FTPConnectionClosedException;
31  import org.apache.commons.net.ftp.FTPReply;
32  
33  /***
34   * This is an example program demonstrating how to use the FTPClient class.
35   * This program connects to an FTP server and retrieves the specified
36   * file.  If the -s flag is used, it stores the local file at the FTP server.
37   * Just so you can see what's happening, all reply strings are printed.
38   * If the -b flag is used, a binary transfer is assumed (default is ASCII).
39   * <p>
40   * Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>
41   * <p>
42   ***/
43  public final class FTPExample
44  {
45  
46      public static final String USAGE =
47          "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" +
48          "\nDefault behavior is to download a file and use ASCII transfer mode.\n" +
49          "\t-s store file on server (upload)\n" +
50          "\t-b use binary transfer mode\n";
51  
52      public static final void main(String[] args)
53      {
54          int base = 0;
55          boolean storeFile = false, binaryTransfer = false, error = false;
56          String server, username, password, remote, local;
57          FTPClient ftp;
58  
59          for (base = 0; base < args.length; base++)
60          {
61              if (args[base].startsWith("-s"))
62                  storeFile = true;
63              else if (args[base].startsWith("-b"))
64                  binaryTransfer = true;
65              else
66                  break;
67          }
68  
69          if ((args.length - base) != 5)
70          {
71              System.err.println(USAGE);
72              System.exit(1);
73          }
74  
75          server = args[base++];
76          username = args[base++];
77          password = args[base++];
78          remote = args[base++];
79          local = args[base];
80  
81          ftp = new FTPClient();
82          ftp.addProtocolCommandListener(new PrintCommandListener(
83                                             new PrintWriter(System.out)));
84  
85          try
86          {
87              int reply;
88              ftp.connect(server);
89              System.out.println("Connected to " + server + ".");
90  
91              // After connection attempt, you should check the reply code to verify
92              // success.
93              reply = ftp.getReplyCode();
94  
95              if (!FTPReply.isPositiveCompletion(reply))
96              {
97                  ftp.disconnect();
98                  System.err.println("FTP server refused connection.");
99                  System.exit(1);
100             }
101         }
102         catch (IOException e)
103         {
104             if (ftp.isConnected())
105             {
106                 try
107                 {
108                     ftp.disconnect();
109                 }
110                 catch (IOException f)
111                 {
112                     // do nothing
113                 }
114             }
115             System.err.println("Could not connect to server.");
116             e.printStackTrace();
117             System.exit(1);
118         }
119 
120 __main:
121         try
122         {
123             if (!ftp.login(username, password))
124             {
125                 ftp.logout();
126                 error = true;
127                 break __main;
128             }
129 
130             System.out.println("Remote system is " + ftp.getSystemName());
131 
132             if (binaryTransfer)
133                 ftp.setFileType(FTP.BINARY_FILE_TYPE);
134 
135             // Use passive mode as default because most of us are
136             // behind firewalls these days.
137             ftp.enterLocalPassiveMode();
138 
139             if (storeFile)
140             {
141                 InputStream input;
142 
143                 input = new FileInputStream(local);
144 
145                 ftp.storeFile(remote, input);
146 
147                 input.close();
148             }
149             else
150             {
151                 OutputStream output;
152 
153                 output = new FileOutputStream(local);
154 
155                 ftp.retrieveFile(remote, output);
156 
157                 output.close();
158             }
159 
160             ftp.logout();
161         }
162         catch (FTPConnectionClosedException e)
163         {
164             error = true;
165             System.err.println("Server closed connection.");
166             e.printStackTrace();
167         }
168         catch (IOException e)
169         {
170             error = true;
171             e.printStackTrace();
172         }
173         finally
174         {
175             if (ftp.isConnected())
176             {
177                 try
178                 {
179                     ftp.disconnect();
180                 }
181                 catch (IOException f)
182                 {
183                     // do nothing
184                 }
185             }
186         }
187 
188         System.exit(error ? 1 : 0);
189     } // end main
190 
191 }
192