1 16 package org.apache.commons.net.ftp; 17 import java.io.BufferedReader ; 18 import java.io.BufferedWriter ; 19 import java.io.IOException ; 20 import java.io.InputStreamReader ; 21 import java.io.OutputStreamWriter ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.net.InetAddress ; 25 import java.net.Socket ; 26 import java.net.SocketException ; 27 import java.util.Enumeration ; 28 import java.util.Vector ; 29 30 import org.apache.commons.net.MalformedServerReplyException; 31 import org.apache.commons.net.ProtocolCommandListener; 32 import org.apache.commons.net.ProtocolCommandSupport; 33 import org.apache.commons.net.SocketClient; 34 import org.apache.commons.net.telnet.TelnetClient; 35 36 97 98 public class FTP extends TelnetClient 99 { 100 101 public static final int DEFAULT_DATA_PORT = 20; 102 103 public static final int DEFAULT_PORT = 21; 104 105 110 public static final int ASCII_FILE_TYPE = 0; 111 112 118 public static final int EBCDIC_FILE_TYPE = 1; 119 120 126 public static final int IMAGE_FILE_TYPE = 2; 127 128 134 public static final int BINARY_FILE_TYPE = 2; 135 136 141 public static final int LOCAL_FILE_TYPE = 3; 142 143 149 public static final int NON_PRINT_TEXT_FORMAT = 4; 150 151 157 public static final int TELNET_TEXT_FORMAT = 5; 158 159 165 public static final int CARRIAGE_CONTROL_TEXT_FORMAT = 6; 166 167 173 public static final int FILE_STRUCTURE = 7; 174 175 180 public static final int RECORD_STRUCTURE = 8; 181 182 188 public static final int PAGE_STRUCTURE = 9; 189 190 196 public static final int STREAM_TRANSFER_MODE = 10; 197 198 203 public static final int BLOCK_TRANSFER_MODE = 11; 204 205 210 public static final int COMPRESSED_TRANSFER_MODE = 12; 211 212 222 public static final String DEFAULT_CONTROL_ENCODING = "ISO-8859-1"; 223 private static final String __modes = "ABILNTCFRPSBC"; 224 225 private StringBuffer __commandBuffer; 226 227 BufferedReader _controlInput; 228 BufferedWriter _controlOutput; 229 int _replyCode; 230 Vector _replyLines; 231 boolean _newReplyString; 232 String _replyString; 233 String _controlEncoding; 234 235 239 protected ProtocolCommandSupport _commandSupport_; 240 241 246 public FTP() 247 { 248 setDefaultPort(DEFAULT_PORT); 249 __commandBuffer = new StringBuffer (); 250 _replyLines = new Vector (); 251 _newReplyString = false; 252 _replyString = null; 253 _commandSupport_ = new ProtocolCommandSupport(this); 254 _controlEncoding = DEFAULT_CONTROL_ENCODING; 255 } 256 257 private void __getReply() throws IOException 258 { 259 int length; 260 261 _newReplyString = true; 262 _replyLines.setSize(0); 263 264 String line = _controlInput.readLine(); 265 266 if (line == null) 267 throw new FTPConnectionClosedException( 268 "Connection closed without indication."); 269 270 length = line.length(); 273 if (length < 3) 274 throw new MalformedServerReplyException( 275 "Truncated server reply: " + line); 276 277 try 278 { 279 String code = line.substring(0, 3); 280 _replyCode = Integer.parseInt(code); 281 } 282 catch (NumberFormatException e) 283 { 284 throw new MalformedServerReplyException( 285 "Could not parse response code.\nServer Reply: " + line); 286 } 287 288 _replyLines.addElement(line); 289 290 if (length > 3 && line.charAt(3) == '-') 292 { 293 do 294 { 295 line = _controlInput.readLine(); 296 297 if (line == null) 298 throw new FTPConnectionClosedException( 299 "Connection closed without indication."); 300 301 _replyLines.addElement(line); 302 303 } 307 while (!(line.length() >= 4 && line.charAt(3) != '-' && 308 Character.isDigit(line.charAt(0)))); 309 } 316 317 if (_commandSupport_.getListenerCount() > 0) 318 _commandSupport_.fireReplyReceived(_replyCode, getReplyString()); 319 320 if (_replyCode == FTPReply.SERVICE_NOT_AVAILABLE) 321 throw new FTPConnectionClosedException( 322 "FTP response 421 received. Server closed connection."); 323 } 324 325 protected void _connectAction_() throws IOException 327 { 328 super._connectAction_(); 329 _controlInput = 330 new BufferedReader (new InputStreamReader (getInputStream(), 331 getControlEncoding())); 332 _controlOutput = 333 new BufferedWriter (new OutputStreamWriter (getOutputStream(), 334 getControlEncoding())); 335 __getReply(); 336 if (FTPReply.isPositivePreliminary(_replyCode)) 338 __getReply(); 339 } 340 341 342 350 public void setControlEncoding(String encoding) { 351 _controlEncoding = encoding; 352 } 353 354 355 359 public String getControlEncoding() { 360 return _controlEncoding; 361 } 362 363 364 370 public void addProtocolCommandListener(ProtocolCommandListener listener) 371 { 372 _commandSupport_.addProtocolCommandListener(listener); 373 } 374 375 381 public void removeProtocolCommandListener(ProtocolCommandListener listener) 382 { 383 _commandSupport_.removeProtocolCommandListener(listener); 384 } 385 386 387 395 public void disconnect() throws IOException 396 { 397 super.disconnect(); 398 _controlInput = null; 399 _controlOutput = null; 400 _replyLines.setSize(0); 401 _newReplyString = false; 402 _replyString = null; 403 } 404 405 406 426 public int sendCommand(String command, String args) throws IOException 427 { 428 String message; 429 430 __commandBuffer.setLength(0); 431 __commandBuffer.append(command); 432 433 if (args != null) 434 { 435 __commandBuffer.append(' '); 436 __commandBuffer.append(args); 437 } 438 __commandBuffer.append(SocketClient.NETASCII_EOL); 439 440 try{ 441 _controlOutput.write(message = __commandBuffer.toString()); 442 _controlOutput.flush(); 443 } 444 catch (SocketException e) 445 { 446 if (!isConnected() || !socketIsConnected(_socket_)) 447 { 448 throw new FTPConnectionClosedException("Connection unexpectedly closed."); 449 } 450 else 451 { 452 throw e; 453 } 454 } 455 456 457 if (_commandSupport_.getListenerCount() > 0) 458 _commandSupport_.fireCommandSent(command, message); 459 460 __getReply(); 461 return _replyCode; 462 } 463 464 471 private boolean socketIsConnected(Socket socket) 472 { 473 if (socket == null) 474 { 475 return false; 476 } 477 478 try 479 { 480 Method isConnected = socket.getClass().getMethod("isConnected", null); 481 return ((Boolean ) isConnected.invoke(socket, null)).booleanValue(); 482 } 483 catch (NoSuchMethodException e) 484 { 485 return true; 486 } 487 catch (IllegalAccessException e) 488 { 489 return true; 490 } 491 catch (InvocationTargetException e) 492 { 493 return true; 494 } 495 } 496 497 518 public int sendCommand(int command, String args) throws IOException 519 { 520 return sendCommand(FTPCommand._commands[command], args); 521 } 522 523 524 542 public int sendCommand(String command) throws IOException 543 { 544 return sendCommand(command, null); 545 } 546 547 548 567 public int sendCommand(int command) throws IOException 568 { 569 return sendCommand(command, null); 570 } 571 572 573 581 public int getReplyCode() 582 { 583 return _replyCode; 584 } 585 586 603 public int getReply() throws IOException 604 { 605 __getReply(); 606 return _replyCode; 607 } 608 609 610 617 public String [] getReplyStrings() 618 { 619 String [] lines; 620 lines = new String [_replyLines.size()]; 621 _replyLines.copyInto(lines); 622 return lines; 623 } 624 625 632 public String getReplyString() 633 { 634 Enumeration en; 635 StringBuffer buffer; 636 637 if (!_newReplyString) 638 return _replyString; 639 640 buffer = new StringBuffer (256); 641 en = _replyLines.elements(); 642 while (en.hasMoreElements()) 643 { 644 buffer.append((String )en.nextElement()); 645 buffer.append(SocketClient.NETASCII_EOL); 646 } 647 648 _newReplyString = false; 649 650 return (_replyString = buffer.toString()); 651 } 652 653 654 668 public int user(String username) throws IOException 669 { 670 return sendCommand(FTPCommand.USER, username); 671 } 672 673 686 public int pass(String password) throws IOException 687 { 688 return sendCommand(FTPCommand.PASS, password); 689 } 690 691 705 public int acct(String account) throws IOException 706 { 707 return sendCommand(FTPCommand.ACCT, account); 708 } 709 710 711 724 public int abor() throws IOException 725 { 726 return sendCommand(FTPCommand.ABOR); 727 } 728 729 743 public int cwd(String directory) throws IOException 744 { 745 return sendCommand(FTPCommand.CWD, directory); 746 } 747 748 761 public int cdup() throws IOException 762 { 763 return sendCommand(FTPCommand.CDUP); 764 } 765 766 779 public int quit() throws IOException 780 { 781 return sendCommand(FTPCommand.QUIT); 782 } 783 784 797 public int rein() throws IOException 798 { 799 return sendCommand(FTPCommand.REIN); 800 } 801 802 816 public int smnt(String dir) throws IOException 817 { 818 return sendCommand(FTPCommand.SMNT, dir); 819 } 820 821 836 public int port(InetAddress host, int port) throws IOException 837 { 838 int num; 839 StringBuffer info = new StringBuffer (24); 840 841 info.append(host.getHostAddress().replace('.', ',')); 842 num = port >>> 8; 843 info.append(','); 844 info.append(num); 845 info.append(','); 846 num = port & 0xff; 847 info.append(num); 848 849 return sendCommand(FTPCommand.PORT, info.toString()); 850 } 851 852 867 public int pasv() throws IOException 868 { 869 return sendCommand(FTPCommand.PASV); 870 } 871 872 889 public int type(int fileType, int formatOrByteSize) throws IOException 890 { 891 StringBuffer arg = new StringBuffer (); 892 893 arg.append(__modes.charAt(fileType)); 894 arg.append(' '); 895 if (fileType == LOCAL_FILE_TYPE) 896 arg.append(formatOrByteSize); 897 else 898 arg.append(__modes.charAt(formatOrByteSize)); 899 900 return sendCommand(FTPCommand.TYPE, arg.toString()); 901 } 902 903 904 919 public int type(int fileType) throws IOException 920 { 921 return sendCommand(FTPCommand.TYPE, 922 __modes.substring(fileType, fileType + 1)); 923 } 924 925 940 public int stru(int structure) throws IOException 941 { 942 return sendCommand(FTPCommand.STRU, 943 __modes.substring(structure, structure + 1)); 944 } 945 946 961 public int mode(int mode) throws IOException 962 { 963 return sendCommand(FTPCommand.MODE, 964 __modes.substring(mode, mode + 1)); 965 } 966 967 984 public int retr(String pathname) throws IOException 985 { 986 return sendCommand(FTPCommand.RETR, pathname); 987 } 988 989 1007 public int stor(String pathname) throws IOException 1008 { 1009 return sendCommand(FTPCommand.STOR, pathname); 1010 } 1011 1012 1028 public int stou() throws IOException 1029 { 1030 return sendCommand(FTPCommand.STOU); 1031 } 1032 1033 1051 public int stou(String pathname) throws IOException 1052 { 1053 return sendCommand(FTPCommand.STOU, pathname); 1054 } 1055 1056 1074 public int appe(String pathname) throws IOException 1075 { 1076 return sendCommand(FTPCommand.APPE, pathname); 1077 } 1078 1079 1093 public int allo(int bytes) throws IOException 1094 { 1095 return sendCommand(FTPCommand.ALLO, Integer.toString(bytes)); 1096 } 1097 1098 1113 public int allo(int bytes, int recordSize) throws IOException 1114 { 1115 return sendCommand(FTPCommand.ALLO, Integer.toString(bytes) + " R " + 1116 Integer.toString(recordSize)); 1117 } 1118 1119 1133 public int rest(String marker) throws IOException 1134 { 1135 return sendCommand(FTPCommand.REST, marker); 1136 } 1137 1138 1152 public int rnfr(String pathname) throws IOException 1153 { 1154 return sendCommand(FTPCommand.RNFR, pathname); 1155 } 1156 1157 1171 public int rnto(String pathname) throws IOException 1172 { 1173 return sendCommand(FTPCommand.RNTO, pathname); 1174 } 1175 1176 1190 public int dele(String pathname) throws IOException 1191 { 1192 return sendCommand(FTPCommand.DELE, pathname); 1193 } 1194 1195 1209 public int rmd(String pathname) throws IOException 1210 { 1211 return sendCommand(FTPCommand.RMD, pathname); 1212 } 1213 1214 1228 public int mkd(String pathname) throws IOException 1229 { 1230 return sendCommand(FTPCommand.MKD, pathname); 1231 } 1232 1233 1246 public int pwd() throws IOException 1247 { 1248 return sendCommand(FTPCommand.PWD); 1249 } 1250 1251 1267 public int list() throws IOException 1268 { 1269 return sendCommand(FTPCommand.LIST); 1270 } 1271 1272 1289 public int list(String pathname) throws IOException 1290 { 1291 return sendCommand(FTPCommand.LIST, pathname); 1292 } 1293 1294 1310 public int nlst() throws IOException 1311 { 1312 return sendCommand(FTPCommand.NLST); 1313 } 1314 1315 1332 public int nlst(String pathname) throws IOException 1333 { 1334 return sendCommand(FTPCommand.NLST, pathname); 1335 } 1336 1337 1351 public int site(String parameters) throws IOException 1352 { 1353 return sendCommand(FTPCommand.SITE, parameters); 1354 } 1355 1356 1369 public int syst() throws IOException 1370 { 1371 return sendCommand(FTPCommand.SYST); 1372 } 1373 1374 1387 public int stat() throws IOException 1388 { 1389 return sendCommand(FTPCommand.STAT); 1390 } 1391 1392 1406 public int stat(String pathname) throws IOException 1407 { 1408 return sendCommand(FTPCommand.STAT, pathname); 1409 } 1410 1411 1424 public int help() throws IOException 1425 { 1426 return sendCommand(FTPCommand.HELP); 1427 } 1428 1429 1443 public int help(String command) throws IOException 1444 { 1445 return sendCommand(FTPCommand.HELP, command); 1446 } 1447 1448 1461 public int noop() throws IOException 1462 { 1463 return sendCommand(FTPCommand.NOOP); 1464 } 1465 1466} 1467 1468 1475 | Popular Tags |