KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > libcheck > FtpCheck


1 /*
2 * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.libcheck;
17
18 import org.apache.commons.net.ftp.FTPClient;
19 import org.apache.commons.net.ftp.FTPFile;
20 import org.apache.commons.net.ftp.FTPReply;
21
22 import java.io.OutputStream JavaDoc;
23
24 /**
25  * Basic check for sftp
26  */

27 public class FtpCheck
28 {
29     public static void main(String JavaDoc args[]) throws Exception JavaDoc
30     {
31         if (args.length < 3)
32         {
33             throw new IllegalArgumentException JavaDoc("Usage: FtpCheck user pass host dir");
34         }
35         String JavaDoc user = args[0];
36         String JavaDoc pass = args[1];
37         String JavaDoc host = args[2];
38         String JavaDoc dir = null;
39         if (args.length == 4)
40         {
41             dir = args[3];
42         }
43
44         FTPClient client = new FTPClient();
45         client.connect(host);
46         int reply = client.getReplyCode();
47         if (!FTPReply.isPositiveCompletion(reply))
48         {
49             throw new IllegalArgumentException JavaDoc("cant connect: " + reply);
50         }
51         if (!client.login(user, pass))
52         {
53             throw new IllegalArgumentException JavaDoc("login failed");
54         }
55         client.enterLocalPassiveMode();
56
57         OutputStream JavaDoc os = client.storeFileStream(dir + "/test.txt");
58         if (os == null)
59         {
60             throw new IllegalStateException JavaDoc(client.getReplyString());
61         }
62         os.write("test".getBytes());
63         os.close();
64         client.completePendingCommand();
65
66         if (dir != null)
67         {
68             if (!client.changeWorkingDirectory(dir))
69             {
70                 throw new IllegalArgumentException JavaDoc("change dir to '" + dir + "' failed");
71             }
72         }
73
74         System.err.println("System: " + client.getSystemName());
75
76         FTPFile[] files = client.listFiles();
77         for (int i = 0; i < files.length; i++)
78         {
79             FTPFile file = files[i];
80             if (file == null)
81             {
82                 System.err.println("#" + i + ": " + null);
83             }
84             else
85             {
86                 System.err.println("#" + i + ": " + file.toString());
87                 System.err.println("\t name:" + file.getName());
88             }
89         }
90         client.disconnect();
91     }
92 }
93
Popular Tags