1 50 51 package com.enterprisedt.net.ftp; 52 53 import java.io.IOException ; 54 import java.io.LineNumberReader ; 55 import java.io.InputStream ; 56 import java.io.InputStreamReader ; 57 import java.io.OutputStream ; 58 import java.io.OutputStreamWriter ; 59 import java.io.PrintWriter ; 60 import java.io.BufferedWriter ; 61 import java.io.FileWriter ; 62 import java.io.DataInputStream ; 63 import java.io.DataOutputStream ; 64 import java.io.BufferedInputStream ; 65 import java.io.BufferedOutputStream ; 66 import java.io.ByteArrayOutputStream ; 67 import java.io.FileOutputStream ; 68 import java.io.FileInputStream ; 69 import java.io.File ; 70 71 import java.text.SimpleDateFormat ; 72 import java.text.ParsePosition ; 73 74 import java.net.InetAddress ; 75 import java.util.Date ; 76 import java.util.Vector ; 77 import java.util.Properties ; 78 79 87 public class FTPClient { 88 89 92 private static String cvsId = "@(#)$Id: FTPClient.java,v 1.1.1.1 2005/06/23 15:23:01 smontoro Exp $"; 93 94 97 private SimpleDateFormat tsFormat = 98 new SimpleDateFormat ("yyyyMMddHHmmss"); 99 100 104 private FTPControlSocket control = null; 105 106 110 private FTPDataSocket data = null; 111 112 116 private int timeout = 0; 117 118 121 private FTPTransferType transferType = FTPTransferType.ASCII; 122 123 127 private FTPConnectMode connectMode = FTPConnectMode.PASV; 128 129 132 private FTPReply lastValidReply; 133 134 140 public FTPClient(String remoteHost) 141 throws IOException , FTPException { 142 143 control = new FTPControlSocket(remoteHost, 144 FTPControlSocket.CONTROL_PORT, 145 null, 0); 146 } 147 148 155 public FTPClient(String remoteHost, int controlPort) 156 throws IOException , FTPException { 157 158 control = new FTPControlSocket(remoteHost, controlPort, null, 0); 159 } 160 161 168 public FTPClient(InetAddress remoteAddr) 169 throws IOException , FTPException { 170 171 control = new FTPControlSocket(remoteAddr, 172 FTPControlSocket.CONTROL_PORT, 173 null, 0); 174 } 175 176 185 public FTPClient(InetAddress remoteAddr, int controlPort) 186 throws IOException , FTPException { 187 188 control = new FTPControlSocket(remoteAddr, controlPort, 189 null, 0); 190 } 191 192 199 public FTPClient(String remoteHost, PrintWriter log, int timeout) 200 throws IOException , FTPException { 201 202 control = new FTPControlSocket(remoteHost, 203 FTPControlSocket.CONTROL_PORT, 204 log, timeout); 205 } 206 207 215 public FTPClient(String remoteHost, int controlPort, 216 PrintWriter log, int timeout) 217 throws IOException , FTPException { 218 219 control = new FTPControlSocket(remoteHost, controlPort, 220 log, timeout); 221 } 222 223 231 public FTPClient(InetAddress remoteAddr, PrintWriter log, 232 int timeout) 233 throws IOException , FTPException { 234 235 control = new FTPControlSocket(remoteAddr, 236 FTPControlSocket.CONTROL_PORT, 237 log, timeout); 238 } 239 240 250 public FTPClient(InetAddress remoteAddr, int controlPort, 251 PrintWriter log, int timeout) 252 throws IOException , FTPException { 253 254 control = new FTPControlSocket(remoteAddr, controlPort, 255 log, timeout); 256 } 257 258 268 public void setTimeout(int millis) 269 throws IOException { 270 271 this.timeout = millis; 272 control.setTimeout(millis); 273 } 274 275 280 public void setConnectMode(FTPConnectMode mode) { 281 connectMode = mode; 282 } 283 284 291 public void login(String user, String password) 292 throws IOException , FTPException { 293 294 String response = control.sendCommand("USER " + user); 295 lastValidReply = control.validateReply(response, "331"); 296 response = control.sendCommand("PASS " + password); 297 lastValidReply = control.validateReply(response, "230"); 298 } 299 300 301 309 public void user(String user) 310 throws IOException , FTPException { 311 312 String reply = control.sendCommand("USER " + user); 313 314 String [] validCodes = {"230", "331"}; 316 lastValidReply = control.validateReply(reply, validCodes); 317 } 318 319 320 328 public void password(String password) 329 throws IOException , FTPException { 330 331 String reply = control.sendCommand("PASS " + password); 332 333 String [] validCodes = {"230", "202"}; 335 lastValidReply = control.validateReply(reply, validCodes); 336 } 337 338 347 public static void initSOCKS(String port, String host) { 348 Properties props = System.getProperties(); 349 props.put("socksProxyPort", port); 350 props.put("socksProxyHost", host); 351 System.setProperties(props); 352 } 353 354 362 public static void initSOCKSAuthentication(String username, 363 String password) { 364 Properties props = System.getProperties(); 365 props.put("java.net.socks.username", username); 366 props.put("java.net.socks.password", password); 367 System.setProperties(props); 368 } 369 370 375 String getRemoteHostName() { 376 return control.getRemoteHostName(); 377 } 378 379 380 386 public void quote(String command, String [] validCodes) 387 throws IOException , FTPException { 388 389 String reply = control.sendCommand(command); 390 391 if (validCodes != null && validCodes.length > 0) 393 lastValidReply = control.validateReply(reply, validCodes); 394 } 395 396 397 405 public void put(String localPath, String remoteFile) 406 throws IOException , FTPException { 407 408 put(localPath, remoteFile, false); 409 } 410 411 419 public void put(InputStream srcStream, String remoteFile) 420 throws IOException , FTPException { 421 422 put(srcStream, remoteFile, false); 423 } 424 425 426 436 public void put(String localPath, String remoteFile, 437 boolean append) 438 throws IOException , FTPException { 439 440 if (getType() == FTPTransferType.ASCII) { 442 putASCII(localPath, remoteFile, append); 443 } 444 else { 445 putBinary(localPath, remoteFile, append); 446 } 447 validateTransfer(); 448 } 449 450 460 public void put(InputStream srcStream, String remoteFile, 461 boolean append) 462 throws IOException , FTPException { 463 464 if (getType() == FTPTransferType.ASCII) { 466 putASCII(srcStream, remoteFile, append); 467 } 468 else { 469 putBinary(srcStream, remoteFile, append); 470 } 471 validateTransfer(); 472 } 473 474 477 private void validateTransfer() 478 throws IOException , FTPException { 479 480 String [] validCodes = {"226", "250"}; 482 String reply = control.readReply(); 483 lastValidReply = control.validateReply(reply, validCodes); 484 } 485 486 493 private void initPut(String remoteFile, boolean append) 494 throws IOException , FTPException { 495 496 data = control.createDataSocket(connectMode); 498 data.setTimeout(timeout); 499 500 String cmd = append ? "APPE " : "STOR "; 502 String reply = control.sendCommand(cmd + remoteFile); 503 504 String [] validCodes = {"125", "150"}; 506 lastValidReply = control.validateReply(reply, validCodes); 507 } 508 509 510 518 private void putASCII(String localPath, String remoteFile, boolean append) 519 throws IOException , FTPException { 520 521 InputStream srcStream = new FileInputStream (localPath); 523 putASCII(srcStream, remoteFile, append); 524 } 525 526 534 private void putASCII(InputStream srcStream, String remoteFile, 535 boolean append) 536 throws IOException , FTPException { 537 538 LineNumberReader in 540 = new LineNumberReader (new InputStreamReader (srcStream)); 541 542 initPut(remoteFile, append); 543 544 BufferedWriter out = 548 new BufferedWriter ( 549 new OutputStreamWriter (data.getOutputStream())); 550 551 String line = null; 554 while ((line = in.readLine()) != null) { 555 out.write(line, 0, line.length()); 556 out.write(FTPControlSocket.EOL, 0, FTPControlSocket.EOL.length()); 557 } 558 in.close(); 559 out.flush(); 560 out.close(); 561 562 try { 564 data.close(); 565 } 566 catch (IOException ignore) {} 567 } 568 569 570 577 private void putBinary(String localPath, String remoteFile, 578 boolean append) 579 throws IOException , FTPException { 580 581 InputStream srcStream = new FileInputStream (localPath); 585 putBinary(srcStream, remoteFile, append); 586 } 587 588 595 private void putBinary(InputStream srcStream, String remoteFile, 596 boolean append) 597 throws IOException , FTPException { 598 599 BufferedInputStream in = 600 new BufferedInputStream (srcStream); 601 602 initPut(remoteFile, append); 603 604 BufferedOutputStream out = 606 new BufferedOutputStream ( 607 new DataOutputStream (data.getOutputStream())); 608 609 byte[] buf = new byte[512]; 610 611 long size = 0; 613 int count = 0; 614 while ((count = in.read(buf)) > 0) { 615 out.write(buf, 0, count); 616 size += count; 617 } 618 619 in.close(); 620 621 out.flush(); 623 out.close(); 624 625 try { 627 data.close(); 628 } 629 catch (IOException ignore) {} 630 631 control.log("Transferred " + size + " bytes to remote host"); 633 } 634 635 636 644 public void put(byte[] bytes, String remoteFile) 645 throws IOException , FTPException { 646 647 put(bytes, remoteFile, false); 648 } 649 650 660 public void put(byte[] bytes, String remoteFile, boolean append) 661 throws IOException , FTPException { 662 663 initPut(remoteFile, append); 664 665 BufferedOutputStream out = 667 new BufferedOutputStream ( 668 new DataOutputStream (data.getOutputStream())); 669 670 out.write(bytes, 0, bytes.length); 672 673 out.flush(); 675 out.close(); 676 677 try { 679 data.close(); 680 } 681 catch (IOException ignore) {} 682 683 validateTransfer(); 684 } 685 686 687 695 public void get(String localPath, String remoteFile) 696 throws IOException , FTPException { 697 698 if (getType() == FTPTransferType.ASCII) { 700 getASCII(localPath, remoteFile); 701 } 702 else { 703 getBinary(localPath, remoteFile); 704 } 705 validateTransfer(); 706 } 707 708 716 public void get(OutputStream destStream, String remoteFile) 717 throws IOException , FTPException { 718 719 if (getType() == FTPTransferType.ASCII) { 721 getASCII(destStream, remoteFile); 722 } 723 else { 724 getBinary(destStream, remoteFile); 725 } 726 validateTransfer(); 727 } 728 729 730 735 private void initGet(String remoteFile) 736 throws IOException , FTPException { 737 738 data = control.createDataSocket(connectMode); 740 data.setTimeout(timeout); 741 742 String reply = control.sendCommand("RETR " + remoteFile); 744 745 String [] validCodes1 = {"125", "150"}; 747 lastValidReply = control.validateReply(reply, validCodes1); 748 } 749 750 751 758 private void getASCII(String localPath, String remoteFile) 759 throws IOException , FTPException { 760 761 initGet(remoteFile); 766 767 File localFile = new File (localPath); 770 771 BufferedWriter out = 773 new BufferedWriter ( 774 new FileWriter (localPath)); 775 776 LineNumberReader in = 780 new LineNumberReader ( 781 new InputStreamReader (data.getInputStream())); 782 783 data.setTimeout(timeout); 789 790 IOException storedEx = null; 792 String line = null; 793 try { 794 while ((line = in.readLine()) != null) { 795 out.write(line, 0, line.length()); 796 out.newLine(); 797 } 798 } 799 catch (IOException ex) { 800 storedEx = ex; 801 localFile.delete(); 802 } 803 finally { 804 out.close(); 805 } 806 807 try { 808 in.close(); 809 data.close(); 810 } 811 catch (IOException ignore) {} 812 813 if (storedEx != null) 815 throw storedEx; 816 } 817 818 825 private void getASCII(OutputStream destStream, String remoteFile) 826 throws IOException , FTPException { 827 828 initGet(remoteFile); 829 830 BufferedWriter out = 832 new BufferedWriter ( 833 new OutputStreamWriter (destStream)); 834 835 LineNumberReader in = 838 new LineNumberReader ( 839 new InputStreamReader (data.getInputStream())); 840 841 data.setTimeout(timeout); 847 848 IOException storedEx = null; 850 String line = null; 851 try { 852 while ((line = in.readLine()) != null) { 853 out.write(line, 0, line.length()); 854 out.newLine(); 855 } 856 } 857 catch (IOException ex) { 858 storedEx = ex; 859 } 860 finally { 861 out.close(); 862 } 863 864 try { 865 in.close(); 866 data.close(); 867 } 868 catch (IOException ignore) {} 869 870 if (storedEx != null) 872 throw storedEx; 873 } 874 875 876 882 private void getBinary(String localPath, String remoteFile) 883 throws IOException , FTPException { 884 885 initGet(remoteFile); 890 891 File localFile = new File (localPath); 894 895 BufferedOutputStream out = 897 new BufferedOutputStream ( 898 new FileOutputStream (localPath, false)); 899 900 BufferedInputStream in = 904 new BufferedInputStream ( 905 new DataInputStream (data.getInputStream())); 906 907 data.setTimeout(timeout); 913 914 long size = 0; 916 int chunksize = 4096; 917 byte [] chunk = new byte[chunksize]; 918 int count; 919 IOException storedEx = null; 920 921 try { 923 while ((count = in.read(chunk, 0, chunksize)) >= 0) { 924 out.write(chunk, 0, count); 925 size += count; 926 } 927 } 928 catch (IOException ex) { 929 storedEx = ex; 930 localFile.delete(); 931 } 932 finally { 933 out.close(); 934 } 935 936 try { 938 in.close(); 939 data.close(); 940 } 941 catch (IOException ignore) {} 942 943 if (storedEx != null) 945 throw storedEx; 946 947 control.log("Transferred " + size + " bytes from remote host"); 949 } 950 951 957 private void getBinary(OutputStream destStream, String remoteFile) 958 throws IOException , FTPException { 959 960 initGet(remoteFile); 961 962 BufferedOutputStream out = 964 new BufferedOutputStream (destStream); 965 966 BufferedInputStream in = 970 new BufferedInputStream ( 971 new DataInputStream (data.getInputStream())); 972 973 data.setTimeout(timeout); 979 980 long size = 0; 982 int chunksize = 4096; 983 byte [] chunk = new byte[chunksize]; 984 int count; 985 IOException storedEx = null; 986 987 try { 989 while ((count = in.read(chunk, 0, chunksize)) >= 0) { 990 out.write(chunk, 0, count); 991 size += count; 992 } 993 } 994 catch (IOException ex) { 995 storedEx = ex; 996 } 997 finally { 998 out.close(); 999 } 1000 1001 try { 1003 in.close(); 1004 data.close(); 1005 } 1006 catch (IOException ignore) {} 1007 1008 if (storedEx != null) 1010 throw storedEx; 1011 1012 control.log("Transferred " + size + " bytes from remote host"); 1014 } 1015 1016 1017 1026 public byte[] get(String remoteFile) 1027 throws IOException , FTPException { 1028 1029 initGet(remoteFile); 1030 1031 BufferedInputStream in = 1033 new BufferedInputStream ( 1034 new DataInputStream (data.getInputStream())); 1035 1036 data.setTimeout(timeout); 1042 1043 int chunksize = 4096; 1045 byte [] chunk = new byte[chunksize]; byte [] resultBuf = null; ByteArrayOutputStream temp = 1048 new ByteArrayOutputStream (chunksize); int count; 1051 while ((count = in.read(chunk, 0, chunksize)) >= 0) { 1053 temp.write(chunk, 0, count); 1054 } 1055 temp.close(); 1056 1057 resultBuf = temp.toByteArray(); 1059 1060 try { 1062 in.close(); 1063 data.close(); 1064 } 1065 catch (IOException ignore) {} 1066 1067 validateTransfer(); 1068 1069 return resultBuf; 1070 } 1071 1072 1073 1082 public boolean site(String command) 1083 throws IOException , FTPException { 1084 1085 String reply = control.sendCommand("SITE " + command); 1087 1088 String [] validCodes = {"200", "202", "502"}; 1091 lastValidReply = control.validateReply(reply, validCodes); 1092 1093 if (reply.substring(0, 3).equals("200")) 1096 return true; 1097 else 1098 return false; 1099 } 1100 1101 1102 1110 public String list(String dirname) 1111 throws IOException , FTPException { 1112 1113 return list(dirname, false); 1114 } 1115 1116 1117 1130 public String list(String dirname, boolean full) 1131 throws IOException , FTPException { 1132 1133 String [] list = dir(dirname, full); 1134 1135 StringBuffer result = new StringBuffer (); 1136 String sep = System.getProperty("line.separator"); 1137 1138 for (int i = 0; i < list.length; i++) { 1140 result.append(list[i]); 1141 result.append(sep); 1142 } 1143 1144 return result.toString(); 1145 } 1146 1147 1153 public String [] dir() 1154 throws IOException , FTPException { 1155 1156 return dir(null, false); 1157 } 1158 1159 1165 public String [] dir(String dirname) 1166 throws IOException , FTPException { 1167 1168 return dir(dirname, false); 1169 } 1170 1171 1172 1184 public String [] dir(String dirname, boolean full) 1185 throws IOException , FTPException { 1186 1187 data = control.createDataSocket(connectMode); 1189 data.setTimeout(timeout); 1190 1191 String command = full ? "LIST ":"NLST "; 1193 if (dirname != null) 1194 command += dirname; 1195 1196 command = command.trim(); 1198 String reply = control.sendCommand(command); 1199 1200 String [] validCodes1 = {"125", "150", "450", "550"}; 1204 lastValidReply = control.validateReply(reply, validCodes1); 1205 1206 String [] result = new String [0]; 1208 1209 String replyCode = lastValidReply.getReplyCode(); 1211 if (!replyCode.equals("450") && !replyCode.equals("550")) { 1212 LineNumberReader in = 1214 new LineNumberReader ( 1215 new InputStreamReader (data.getInputStream())); 1216 1217 Vector lines = new Vector (); 1219 String line = null; 1220 while ((line = in.readLine()) != null) { 1221 lines.add(line); 1222 } 1223 try { 1224 in.close(); 1225 data.close(); 1226 } 1227 catch (IOException ignore) {} 1228 1229 String [] validCodes2 = {"226", "250"}; 1231 reply = control.readReply(); 1232 lastValidReply = control.validateReply(reply, validCodes2); 1233 1234 if (!lines.isEmpty()) 1236 result = (String [])lines.toArray(result); 1237 } 1238 return result; 1239 } 1240 1241 1246 public FTPReply getLastValidReply() { 1247 return lastValidReply; 1248 } 1249 1250 1251 1257 public void debugResponses(boolean on) { 1258 control.debugResponses(on); 1259 } 1260 1261 1267 public void setLogStream(PrintWriter log) { 1268 control.setLogStream(log); 1269 } 1270 1271 1277 public FTPTransferType getType() { 1278 return transferType; 1279 } 1280 1281 1287 public void setType(FTPTransferType type) 1288 throws IOException , FTPException { 1289 1290 String typeStr = FTPTransferType.ASCII_CHAR; 1292 if (type.equals(FTPTransferType.BINARY)) 1293 typeStr = FTPTransferType.BINARY_CHAR; 1294 1295 String reply = control.sendCommand("TYPE " + typeStr); 1297 lastValidReply = control.validateReply(reply, "200"); 1298 1299 transferType = type; 1301 } 1302 1303 1304 1310 public void delete(String remoteFile) 1311 throws IOException , FTPException { 1312 1313 String reply = control.sendCommand("DELE " + remoteFile); 1314 lastValidReply = control.validateReply(reply, "250"); 1315 } 1316 1317 1318 1324 public void rename(String from, String to) 1325 throws IOException , FTPException { 1326 1327 String reply = control.sendCommand("RNFR " + from); 1328 lastValidReply = control.validateReply(reply, "350"); 1329 1330 reply = control.sendCommand("RNTO " + to); 1331 lastValidReply = control.validateReply(reply, "250"); 1332 } 1333 1334 1335 1341 public void rmdir(String dir) 1342 throws IOException , FTPException { 1343 1344 String reply = control.sendCommand("RMD " + dir); 1345 1346 String [] validCodes = {"250", "257"}; 1349 lastValidReply = control.validateReply(reply, validCodes); 1350 } 1351 1352 1353 1359 public void mkdir(String dir) 1360 throws IOException , FTPException { 1361 1362 String reply = control.sendCommand("MKD " + dir); 1363 lastValidReply = control.validateReply(reply, "257"); 1364 } 1365 1366 1367 1374 public void chdir(String dir) 1375 throws IOException , FTPException { 1376 1377 String reply = control.sendCommand("CWD " + dir); 1378 lastValidReply = control.validateReply(reply, "250"); 1379 } 1380 1381 1387 public Date modtime(String remoteFile) 1388 throws IOException , FTPException { 1389 1390 String reply = control.sendCommand("MDTM " + remoteFile); 1391 lastValidReply = control.validateReply(reply, "213"); 1392 1393 Date ts = tsFormat.parse(lastValidReply.getReplyText(), 1395 new ParsePosition (0)); 1396 return ts; 1397 } 1398 1399 1404 public String pwd() 1405 throws IOException , FTPException { 1406 1407 String reply = control.sendCommand("PWD"); 1408 lastValidReply = control.validateReply(reply, "257"); 1409 1410 String text = lastValidReply.getReplyText(); 1414 int start = text.indexOf('"'); 1415 int end = text.lastIndexOf('"'); 1416 if (start >= 0 && end > start) 1417 return text.substring(start+1, end); 1418 else 1419 return text; 1420 } 1421 1422 1427 public String system() 1428 throws IOException , FTPException { 1429 1430 String reply = control.sendCommand("SYST"); 1431 lastValidReply = control.validateReply(reply, "215"); 1432 return lastValidReply.getReplyText(); 1433 } 1434 1435 1441 public String help(String command) 1442 throws IOException , FTPException { 1443 1444 String reply = control.sendCommand("HELP " + command); 1445 String [] validCodes = {"211", "214"}; 1446 lastValidReply = control.validateReply(reply, validCodes); 1447 return lastValidReply.getReplyText(); 1448 } 1449 1450 1454 public void quit() 1455 throws IOException , FTPException { 1456 1457 try { 1458 String reply = control.sendCommand("QUIT"); 1459 String [] validCodes = {"221", "226"}; 1460 lastValidReply = control.validateReply(reply, validCodes); 1461 } 1462 finally { control.logout(); 1464 control = null; 1465 } 1466 } 1467 1468} 1469 1470 1471 1472 | Popular Tags |