1 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 ; 23 24 27 public class FtpCheck 28 { 29 public static void main(String args[]) throws Exception 30 { 31 if (args.length < 3) 32 { 33 throw new IllegalArgumentException ("Usage: FtpCheck user pass host dir"); 34 } 35 String user = args[0]; 36 String pass = args[1]; 37 String host = args[2]; 38 String 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 ("cant connect: " + reply); 50 } 51 if (!client.login(user, pass)) 52 { 53 throw new IllegalArgumentException ("login failed"); 54 } 55 client.enterLocalPassiveMode(); 56 57 OutputStream os = client.storeFileStream(dir + "/test.txt"); 58 if (os == null) 59 { 60 throw new IllegalStateException (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 ("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 |