1 16 package examples; 17 18 import java.io.FileInputStream ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.io.PrintWriter ; 24 import org.apache.commons.net.ftp.FTP; 25 import org.apache.commons.net.ftp.FTPClient; 26 import org.apache.commons.net.ftp.FTPConnectionClosedException; 27 import org.apache.commons.net.ftp.FTPReply; 28 29 39 public final class ftp 40 { 41 42 public static final String USAGE = 43 "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" + 44 "\nDefault behavior is to download a file and use ASCII transfer mode.\n" + 45 "\t-s store file on server (upload)\n" + 46 "\t-b use binary transfer mode\n"; 47 48 public static final void main(String [] args) 49 { 50 int base = 0; 51 boolean storeFile = false, binaryTransfer = false, error = false; 52 String server, username, password, remote, local; 53 FTPClient ftp; 54 55 for (base = 0; base < args.length; base++) 56 { 57 if (args[base].startsWith("-s")) 58 storeFile = true; 59 else if (args[base].startsWith("-b")) 60 binaryTransfer = true; 61 else 62 break; 63 } 64 65 if ((args.length - base) != 5) 66 { 67 System.err.println(USAGE); 68 System.exit(1); 69 } 70 71 server = args[base++]; 72 username = args[base++]; 73 password = args[base++]; 74 remote = args[base++]; 75 local = args[base]; 76 77 ftp = new FTPClient(); 78 ftp.addProtocolCommandListener(new PrintCommandListener( 79 new PrintWriter (System.out))); 80 81 try 82 { 83 int reply; 84 ftp.connect(server); 85 System.out.println("Connected to " + server + "."); 86 87 reply = ftp.getReplyCode(); 90 91 if (!FTPReply.isPositiveCompletion(reply)) 92 { 93 ftp.disconnect(); 94 System.err.println("FTP server refused connection."); 95 System.exit(1); 96 } 97 } 98 catch (IOException e) 99 { 100 if (ftp.isConnected()) 101 { 102 try 103 { 104 ftp.disconnect(); 105 } 106 catch (IOException f) 107 { 108 } 110 } 111 System.err.println("Could not connect to server."); 112 e.printStackTrace(); 113 System.exit(1); 114 } 115 116 __main: 117 try 118 { 119 if (!ftp.login(username, password)) 120 { 121 ftp.logout(); 122 error = true; 123 break __main; 124 } 125 126 System.out.println("Remote system is " + ftp.getSystemName()); 127 128 if (binaryTransfer) 129 ftp.setFileType(FTP.BINARY_FILE_TYPE); 130 131 ftp.enterLocalPassiveMode(); 134 135 if (storeFile) 136 { 137 InputStream input; 138 139 input = new FileInputStream (local); 140 141 ftp.storeFile(remote, input); 142 143 input.close(); 144 } 145 else 146 { 147 OutputStream output; 148 149 output = new FileOutputStream (local); 150 151 ftp.retrieveFile(remote, output); 152 153 output.close(); 154 } 155 156 ftp.logout(); 157 } 158 catch (FTPConnectionClosedException e) 159 { 160 error = true; 161 System.err.println("Server closed connection."); 162 e.printStackTrace(); 163 } 164 catch (IOException e) 165 { 166 error = true; 167 e.printStackTrace(); 168 } 169 finally 170 { 171 if (ftp.isConnected()) 172 { 173 try 174 { 175 ftp.disconnect(); 176 } 177 catch (IOException f) 178 { 179 } 181 } 182 } 183 184 System.exit(error ? 1 : 0); 185 } 187 } 188 189 | Popular Tags |