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  package org.apache.commons.vfs2.libcheck;
18  
19  import java.io.OutputStream;
20  import java.nio.charset.Charset;
21  
22  import org.apache.commons.net.ftp.FTPClient;
23  import org.apache.commons.net.ftp.FTPFile;
24  import org.apache.commons.net.ftp.FTPReply;
25  
26  /**
27   * Basic check for FTP.
28   */
29  public final class FtpCheck {
30  
31      private static final int MAX_ARG_COUNT = 4;
32  
33      /**
34       * Invokes this example from the command line.
35       *
36       * @param args Arguments TODO
37       * @throws Exception If anything goes wrong.
38       */
39      public static void main(final String[] args) throws Exception {
40          if (args.length < 3) {
41              throw new IllegalArgumentException("Usage: FtpCheck user pass host dir");
42          }
43          final String user = args[0];
44          final String pass = args[1];
45          final String host = args[2];
46          String dir = null;
47          if (args.length == MAX_ARG_COUNT) {
48              dir = args[3];
49          }
50  
51          final FTPClient client = new FTPClient();
52          client.connect(host);
53          final int reply = client.getReplyCode();
54          if (!FTPReply.isPositiveCompletion(reply)) {
55              throw new IllegalArgumentException("cant connect: " + reply);
56          }
57          if (!client.login(user, pass)) {
58              throw new IllegalArgumentException("login failed");
59          }
60          client.enterLocalPassiveMode();
61  
62          final OutputStream os = client.storeFileStream(dir + "/test.txt");
63          if (os == null) {
64              throw new IllegalStateException(client.getReplyString());
65          }
66          os.write("test".getBytes(Charset.defaultCharset()));
67          os.close();
68          client.completePendingCommand();
69  
70          if (dir != null && !client.changeWorkingDirectory(dir)) {
71              throw new IllegalArgumentException("change dir to '" + dir + "' failed");
72          }
73  
74          System.err.println("System: " + client.getSystemType());
75  
76          final FTPFile[] files = client.listFiles();
77          for (int i = 0; i < files.length; i++) {
78              final FTPFile file = files[i];
79              if (file == null) {
80                  System.err.println("#" + i + ": " + null);
81              } else {
82                  System.err.println("#" + i + ": " + file.getRawListing());
83                  System.err.println("#" + i + ": " + file);
84                  System.err.println("\t name:" + file.getName() + " type:" + file.getType());
85              }
86          }
87          client.disconnect();
88      }
89  
90      private FtpCheck() {
91          /* main class not instantiated. */
92      }
93  }