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      private FtpCheck() {
31          /* main class not instantiated. */
32      }
33  
34      public static void main(final String[] args) throws Exception {
35          if (args.length < 3) {
36              throw new IllegalArgumentException("Usage: FtpCheck user pass host dir");
37          }
38          final String user = args[0];
39          final String pass = args[1];
40          final String host = args[2];
41          String dir = null;
42          if (args.length == 4) {
43              dir = args[3];
44          }
45  
46          final FTPClient client = new FTPClient();
47          client.connect(host);
48          final int reply = client.getReplyCode();
49          if (!FTPReply.isPositiveCompletion(reply)) {
50              throw new IllegalArgumentException("cant connect: " + reply);
51          }
52          if (!client.login(user, pass)) {
53              throw new IllegalArgumentException("login failed");
54          }
55          client.enterLocalPassiveMode();
56  
57          final OutputStream os = client.storeFileStream(dir + "/test.txt");
58          if (os == null) {
59              throw new IllegalStateException(client.getReplyString());
60          }
61          os.write("test".getBytes(Charset.defaultCharset()));
62          os.close();
63          client.completePendingCommand();
64  
65          if (dir != null && !client.changeWorkingDirectory(dir)) {
66              throw new IllegalArgumentException("change dir to '" + dir + "' failed");
67          }
68  
69          System.err.println("System: " + client.getSystemType());
70  
71          final FTPFile[] files = client.listFiles();
72          for (int i = 0; i < files.length; i++) {
73              final FTPFile file = files[i];
74              if (file == null) {
75                  System.err.println("#" + i + ": " + null);
76              } else {
77                  System.err.println("#" + i + ": " + file.getRawListing());
78                  System.err.println("#" + i + ": " + file.toString());
79                  System.err.println("\t name:" + file.getName() + " type:" + file.getType());
80              }
81          }
82          client.disconnect();
83      }
84  }