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  import java.security.NoSuchAlgorithmException;
27  
28  import org.apache.commons.net.PrintCommandListener;
29  import org.apache.commons.net.ftp.FTP;
30  import org.apache.commons.net.ftp.FTPConnectionClosedException;
31  import org.apache.commons.net.ftp.FTPReply;
32  import org.apache.commons.net.ftp.FTPSClient;
33  
34  /***
35   * This is an example program demonstrating how to use the FTPSClient class.
36   * This program connects to an FTP server and retrieves the specified
37   * file.  If the -s flag is used, it stores the local file at the FTP server.
38   * Just so you can see what's happening, all reply strings are printed.
39   * If the -b flag is used, a binary transfer is assumed (default is ASCII).
40   * <p>
41   * Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>
42   * <p>
43   ***/
44  public final class FTPSExample
45  {
46  
47      public static final String USAGE =
48          "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" +
49          "\nDefault behavior is to download a file and use ASCII transfer mode.\n" +
50          "\t-s store file on server (upload)\n" +
51          "\t-b use binary transfer mode\n";
52  
53      public static final void main(String[] args) throws NoSuchAlgorithmException
54      {
55          int base = 0;
56          boolean storeFile = false, binaryTransfer = false, error = false;
57          String server, username, password, remote, local;
58          String protocol = "SSL";    // SSL/TLS
59          FTPSClient ftps;
60          
61          for (base = 0; base < args.length; base++)
62          {
63              if (args[base].startsWith("-s"))
64                  storeFile = true;
65              else if (args[base].startsWith("-b"))
66                  binaryTransfer = true;
67              else
68                  break;
69          }
70  
71          if ((args.length - base) != 5)
72          {
73              System.err.println(USAGE);
74              System.exit(1);
75          }
76  
77          server = args[base++];
78          username = args[base++];
79          password = args[base++];
80          remote = args[base++];
81          local = args[base];
82  
83          ftps = new FTPSClient(protocol);
84         
85          ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
86  
87          try
88          {
89              int reply;
90  
91              ftps.connect(server);
92              System.out.println("Connected to " + server + ".");
93  
94              // After connection attempt, you should check the reply code to verify
95              // success.
96              reply = ftps.getReplyCode();
97  
98              if (!FTPReply.isPositiveCompletion(reply))
99              {
100                 ftps.disconnect();
101                 System.err.println("FTP server refused connection.");
102                 System.exit(1);
103             }
104         }
105         catch (IOException e)
106         {
107             if (ftps.isConnected())
108             {
109                 try
110                 {
111                     ftps.disconnect();
112                 }
113                 catch (IOException f)
114                 {
115                     // do nothing
116                 }
117             }
118             System.err.println("Could not connect to server.");
119             e.printStackTrace();
120             System.exit(1);
121         }
122 
123 __main:
124         try
125         {
126             ftps.setBufferSize(1000);
127 
128             if (!ftps.login(username, password))
129             {
130                 ftps.logout();
131                 error = true;
132                 break __main;
133             }
134 
135             
136             System.out.println("Remote system is " + ftps.getSystemName());
137 
138             if (binaryTransfer) ftps.setFileType(FTP.BINARY_FILE_TYPE);
139 
140             // Use passive mode as default because most of us are
141             // behind firewalls these days.
142             ftps.enterLocalPassiveMode();
143 
144             if (storeFile)
145             {
146                 InputStream input;
147 
148                 input = new FileInputStream(local);
149 
150                 ftps.storeFile(remote, input);
151 
152                 input.close();
153             }
154             else
155             {
156                 OutputStream output;
157 
158                 output = new FileOutputStream(local);
159 
160                 ftps.retrieveFile(remote, output);
161 
162                 output.close();
163             }
164 
165             ftps.logout();
166         }
167         catch (FTPConnectionClosedException e)
168         {
169             error = true;
170             System.err.println("Server closed connection.");
171             e.printStackTrace();
172         }
173         catch (IOException e)
174         {
175             error = true;
176             e.printStackTrace();
177         }
178         finally
179         {
180             if (ftps.isConnected())
181             {
182                 try
183                 {
184                     ftps.disconnect();
185                 }
186                 catch (IOException f)
187                 {
188                     // do nothing
189                 }
190             }
191         }
192 
193         System.exit(error ? 1 : 0);
194     } // end main
195 
196 }