01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.vfs.libcheck;
18:
19: import org.apache.commons.net.ftp.FTPClient;
20: import org.apache.commons.net.ftp.FTPFile;
21: import org.apache.commons.net.ftp.FTPReply;
22:
23: import java.io.OutputStream;
24:
25: /**
26: * Basic check for sftp
27: */
28: public class FtpCheck {
29: public static void main(String args[]) throws Exception {
30: if (args.length < 3) {
31: throw new IllegalArgumentException(
32: "Usage: FtpCheck user pass host dir");
33: }
34: String user = args[0];
35: String pass = args[1];
36: String host = args[2];
37: String dir = null;
38: if (args.length == 4) {
39: dir = args[3];
40: }
41:
42: FTPClient client = new FTPClient();
43: client.connect(host);
44: int reply = client.getReplyCode();
45: if (!FTPReply.isPositiveCompletion(reply)) {
46: throw new IllegalArgumentException("cant connect: " + reply);
47: }
48: if (!client.login(user, pass)) {
49: throw new IllegalArgumentException("login failed");
50: }
51: client.enterLocalPassiveMode();
52:
53: OutputStream os = client.storeFileStream(dir + "/test.txt");
54: if (os == null) {
55: throw new IllegalStateException(client.getReplyString());
56: }
57: os.write("test".getBytes());
58: os.close();
59: client.completePendingCommand();
60:
61: if (dir != null) {
62: if (!client.changeWorkingDirectory(dir)) {
63: throw new IllegalArgumentException("change dir to '"
64: + dir + "' failed");
65: }
66: }
67:
68: System.err.println("System: " + client.getSystemName());
69:
70: FTPFile[] files = client.listFiles();
71: for (int i = 0; i < files.length; i++) {
72: FTPFile file = files[i];
73: if (file == null) {
74: System.err.println("#" + i + ": " + null);
75: } else {
76: System.err.println("#" + i + ": "
77: + file.getRawListing());
78: System.err.println("#" + i + ": " + file.toString());
79: System.err.println("\t name:" + file.getName()
80: + " type:" + file.getType());
81: }
82: }
83: client.disconnect();
84: }
85: }
|