FTPSrv.java 17.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
package com.sitech.util.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.sitech.util.SysHelper;
public class FTPSrv implements FTPIF{
	private FTPDaemon ftpDaemon = null;

	public int timeout;
	public int port;
	public String host;
	public String user;
	public String password;
	public String encode;
	public boolean isPassiveMode = false;
	
	public FTPSrv(){
		ftpDaemon = new FTPDaemon();
		ftpDaemon.start();
	}

	/**
	 * 登陆FTP
	 * @param host
	 * @param port
	 * @param user
	 * @param pwd
	 * @param encode
	 * @param timeout
	 * @throws Exception
	 */
	public void login(String host, int port, String user, String pwd, String encode, int timeout) throws Exception {
		this.host = host;
		this.port = port;
		this.user = user;
		this.password = pwd;
		this.encode = encode;
		this.timeout = timeout;
		String[] args = new String[]{host, String.valueOf(port), user, pwd, encode, String.valueOf(timeout)};
		ftpDaemon.doIt(FTPDaemon.LOGIN, args);
		getReturnValue("login", timeout);
	}

	/**
	 * 登陆FTP(默认端口21、编码GBK、超时2分钟)
	 * @param host
	 * @param user
	 * @param pwd
	 * @throws Exception
	 */
	public void login(String host, String user, String pwd) throws Exception {
		login(host, 21, user, pwd, "GBK", 1000 * 60 * 2);
	}
	
	/**
	 * 登出FTP
	 * @param immediate 是否立即关闭连接
	 * @param timeout 超时时间(单位:毫秒)
	 * @throws Exception
	 */
	public void logout(boolean immediate, int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.LOGOUT, new String[]{String.valueOf(immediate)});
		try{
			getReturnValue("logout", timeout);
		}catch(Exception e){
			throw e;
		}finally{
			ftpDaemon.interrupt();
		}
	}
	
	/**
	 * 登出FTP(默认超时10秒)
	 * @throws Exception
	 */
	public void logout() throws Exception {
		logout(false, 1000*30);
	}
	
	/**
	 * 改为被动模式
	 * @param timeout 超时时间(单位:毫秒)
	 * @throws Exception
	 */
	public void entryPassiveMode(int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.ENTRY_PASSIVE_MODE, null);
		getReturnValue("entryPassiveMode", timeout);
		isPassiveMode = true;
	}
	
	/**
	 * 改为被动模式(默认超时10秒)
	 * @throws Exception
	 */
	public void entryPassiveMode() throws Exception {
		entryPassiveMode(1000*30);
	}
	
	/**
	 * 获取远程主机IP
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 */
	public String getRemoteHostIp(long timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.GET_REMOTE_HOST_IP, null);
		return getReturnValue("getRemoteHostIp", timeout);
	}
	
	/**
	 * 获取远程主机IP(默认超时10秒)
	 * @return
	 * @throws Exception
	 */
	public String getRemoteHostIp() throws Exception {
		return getRemoteHostIp(1000*30);
	}
	
	/**
	 * 获取encoding
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public String getEncoding(long timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.GET_ENCODING, null);
		return getReturnValue("getEncoding", timeout);
	}
	
	/**
	 * 获取encoding(默认超时10秒)
	 * @return
	 * @throws Exception
	 */
	public String getEncoding() throws Exception {
		return getEncoding(1000*30);
	}
	
	/**
	 * 删除指定目录
	 * @param dir
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public boolean rmdir(String dir, int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.REMOVE_DIR, new String[]{dir});
		if("true".equalsIgnoreCase(getReturnValue("rmdir", timeout))){
			return true;
		}
		return false;
	}
	
	/**
	 * 删除指定目录(默认超时10秒)
	 * @param dir
	 * @return
	 * @throws Exception
	 */
	public boolean rmdir(String dir) throws Exception {
		return rmdir(dir, 1000*30);
	}
	
	/**
	 * 删除指定文件
	 * @param filepath
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public boolean rmfile(String filepath, int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.REMOVE_FILE, new String[]{filepath});
		if("true".equalsIgnoreCase(getReturnValue("rmfile", timeout))){
			return true;
		}
		return false;
	}
	
	/**
	 * 删除指定文件(默认超时10秒)
	 * @param filepath
	 * @return
	 * @throws Exception
	 */
	public boolean rmfile(String filepath) throws Exception {
		return rmfile(filepath, 1000*30);
	}
	
	/**
	 * 创建目录
	 * @param dir
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public boolean mkdir(String dir, int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.MAKE_DIR, new String[]{dir});
		if("true".equalsIgnoreCase(getReturnValue("mkdir", timeout))){
			return true;
		}
		return false;
	}
	
	/**
	 * 创建目录(默认超时10秒)
	 * @param dir
	 * @return
	 * @throws Exception
	 */
	public boolean mkdir(String dir) throws Exception {
		return mkdir(dir, 1000*30);
	}
	
	/**
	 * 改变当前工作目录
	 * @param dir
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public boolean chdir(String dir, int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.CHANGE_DIR, new String[]{dir});
		if("true".equalsIgnoreCase(getReturnValue("chdir", timeout))){
			return true;
		}
		return false;
	}
	
	/**
	 * 改变当前工作目录(默认超时10秒)
	 * @param dir
	 * @return
	 * @throws Exception
	 */
	public boolean chdir(String dir) throws Exception {
		return chdir(dir, 1000*30);
	}
	
	/**
	 * 得到当前路径
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public String pwd(int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.PWD, null);
		return getReturnValue("pwd", timeout);
	}
	
	/**
	 * 得到当前路径(默认超时10秒)
	 * @return
	 * @throws Exception
	 */
	public String pwd() throws Exception {
		return pwd(1000*30);
	}
	
	/**
	 * 列出当前工作目录下的所有文件
	 * @param expr 文件名表达式
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public AFtpRemoteFile[] list(String expr, long timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.LIST, new String[]{expr});
		return getFtpRemoteFiles("list", timeout);
	}

	/**
	 * 列出当前工作目录下的所有文件
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public AFtpRemoteFile[] list(long timeout) throws Exception {
		return list(null, timeout);
	}
	
	/**
	 * 列出当前工作目录下的所有文件(默认10分钟)
	 * @return
	 * @throws Exception
	 */
	public AFtpRemoteFile[] list() throws Exception {
		return list(null, 1000*60*10);
	}
	
	/**
	 * 获取远程指定文件
	 * @param fileName
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public AFtpRemoteFile getAFtpRemoteFile(String fileName, int timeout)throws Exception {
		ftpDaemon.doIt(FTPDaemon.GET_REMOTE_FILE, new String[]{fileName});
		long startTime = System.currentTimeMillis();
		while(true){
			SysHelper.waitIt(this, 50);
			if(!ftpDaemon.isDoIt()){
				if(ftpDaemon.getException() != null){
					ftpDaemon.interrupt();
					throw ftpDaemon.getException();
				}else{
					return ftpDaemon.getFtpRemoteFile();
				}
			}
			
			/** 检查是否超时 */
			if(System.currentTimeMillis()-startTime > timeout){
				ftpDaemon.interrupt();
				throw new Exception("ftp operation[getAFtpRemoteFile] timeout [" + timeout + "] milliseconds");
			}
		}
	}
	
	/**
	 * 获取远程指定文件(默认10分钟)
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public AFtpRemoteFile getAFtpRemoteFile(String fileName)throws Exception {
		return getAFtpRemoteFile(fileName, 1000*60*10);
	}
	
	/**
	 * 列出当前工作目录下的所有文件
	 * @param expr
	 * @param fast
	 * @param currDir
	 * @param timeOffset
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public AFtpRemoteFile[] nlst(String expr, boolean fast, String currDir, long timeOffset, long timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.NLST, new String[]{expr, String.valueOf(fast), currDir, String.valueOf(timeOffset)});
		return getFtpRemoteFiles("nlst", timeout);
	}
	
	/**
	 * 列出当前工作目录下的所有文件(默认超时10分钟)
	 * @param expr
	 * @return
	 * @throws IOException
	 */
	public AFtpRemoteFile[] nlst(String expr) throws Exception {
		return nlst(expr, false, null, -1, 1000*60*10);
	}
	
	public AFtpRemoteFile[] nlst(String expr, long timeout) throws Exception {
		return nlst(expr, false, null, -1, timeout);
	}
	
	/**
	 * 上传文件
	 * @param lfileInput
	 * @param toRfileName
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public boolean store(InputStream lfileInput, String toRfileName, int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.STORE, new String[]{toRfileName}, lfileInput);
		if("true".equalsIgnoreCase(getReturnValue("store", timeout))){
			return true;
		}
		return false;
	}
	
	/**
	 * 上传文件(默认超时10分钟)
	 * @param lfileInput
	 * @param toRfileName
	 * @return
	 * @throws Exception
	 */
	public boolean store(InputStream lfileInput, String toRfileName) throws Exception {
		return store(lfileInput, toRfileName, 1000*60*10);
	}
	
	/**
	 * 上传文件
	 * @param lfileName
	 * @param toRfileName
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public boolean store(String lfileName, String toRfileName, int timeout) throws Exception {
		FileInputStream lfileInput = null;
		try {
			lfileInput = new FileInputStream(lfileName);
			return store(lfileInput, toRfileName, timeout);
		} catch (Exception ioe) {
			throw ioe;
		} finally {
			if (lfileInput != null)
				lfileInput.close();
		}
	}
	
	/**
	 * 上传文件(默认超时10分钟)
	 * @param lfileName
	 * @param toRfileName
	 * @return
	 * @throws Exception
	 */
	public boolean store(String lfileName, String toRfileName) throws Exception {
		return store(lfileName, toRfileName, 1000*60*10);
	}
	
	/**
	 * 上传文件(默认超时10分钟,文件名与本地文件相同)
	 * @param lfileName
	 * @return
	 * @throws Exception
	 */
	public boolean store(String lfileName) throws Exception {
		return store(lfileName, new File(lfileName).getName());
	}
	
	/**
	 * 下载文件
	 * @param rfileName
	 * @param toLfileOutput
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public boolean retrive(String rfileName, OutputStream toLfileOutput, int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.RETRIVE, new String[]{rfileName}, toLfileOutput);
		if("true".equalsIgnoreCase(getReturnValue("retrive", timeout))){
			return true;
		}
		return false;
	}
	
	/**
	 * 下载文件(默认超时10分钟)
	 * @param rfileName
	 * @param toLfileOutput
	 * @return
	 * @throws Exception
	 */
	public boolean retrive(String rfileName, OutputStream toLfileOutput) throws Exception {
		return retrive(rfileName, toLfileOutput, 1000*60*10);
	}
	
	/**
	 * 下载文件
	 * @param rfileName
	 * @param toLfileName
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public boolean retrive(String rfileName, String toLfileName, int timeout) throws Exception {
		FileOutputStream toLfileOutput = null;
		try {
			toLfileOutput = new FileOutputStream(toLfileName);
			return retrive(rfileName, toLfileOutput, timeout);
		} catch (Exception ioe) {
			throw ioe;
		} finally {
			if (toLfileOutput != null)
				toLfileOutput.close();
		}
	}
	
	/**
	 * 下载文件(默认超时10分钟)
	 * @param rfileName
	 * @param toLfileName
	 * @return
	 * @throws Exception
	 */
	public boolean retrive(String rfileName, String toLfileName) throws Exception {
		return retrive(rfileName, toLfileName, 1000*60*10);
	}

	/**
	 * 下载文件(默认超时10分钟,文件名与远程文件相同)
	 * @param rfileName
	 * @return
	 * @throws Exception
	 */
	public boolean retrive(String rfileName) throws Exception {
		return retrive(rfileName, new File(rfileName).getName());
	}
	
	public boolean rename(String from, String to) throws Exception{
		ftpDaemon.doIt(FTPDaemon.RENAME_FILE, new String[] { from, to });
		if ("true".equalsIgnoreCase(getReturnValue("rename", timeout))) {
			return true;
		}
		return false;		
	}
	
	/**
	 * 获取远程输入流
	 * @param rfileName
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public InputStream retriveByStream(String rfileName, int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.RETRIVE_BY_STREAM, new String[]{rfileName});
		long startTime = System.currentTimeMillis();
		while(true){
			SysHelper.waitIt(this, 50);
			if(!ftpDaemon.isDoIt()){
				if(ftpDaemon.getException() != null){
					ftpDaemon.interrupt();
					throw ftpDaemon.getException();
				}else{
					return ftpDaemon.getInputStream();
				}
			}
			
			/** 检查是否超时 */
			if(System.currentTimeMillis()-startTime > timeout){
				ftpDaemon.interrupt();
				throw new Exception("ftp operation[retriveByStream] timeout [" + timeout + "] milliseconds");
			}
		}
	}
	
	/**
	 * 获取远程输入流(默认超时10分钟)
	 * @param rfileName
	 * @return
	 * @throws Exception
	 */
	public InputStream retriveByStream(String rfileName) throws Exception {
		return retriveByStream(rfileName, 1000*60*10);
	}
	
	/**
	 * 获取远程输出流
	 * @param rfileName
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public OutputStream storeByStream(String rfileName, int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.STORE_BY_STREAM, new String[]{rfileName});
		long startTime = System.currentTimeMillis();
		while(true){
			SysHelper.waitIt(this, 50);
			if(!ftpDaemon.isDoIt()){
				if(ftpDaemon.getException() != null){
					ftpDaemon.interrupt();
					throw ftpDaemon.getException();
				}else{
					return ftpDaemon.getOutputStream();
				}
			}
			
			/** 检查是否超时 */
			if(System.currentTimeMillis()-startTime > timeout){
				ftpDaemon.interrupt();
				throw new Exception("ftp operation[storeByStream] timeout [" + timeout + "] milliseconds");
			}
		}
	}
	
	/**
	 * 获取远程输出流(默认超时10分钟)
	 * @param rfileName
	 * @return
	 * @throws Exception
	 */
	public OutputStream storeByStream(String rfileName) throws Exception {
		return storeByStream(rfileName, 1000*60*10);
	}
	
	/**
	 * 命令结束
	 * @param timeout 超时时间(单位:毫秒)
	 * @return
	 * @throws Exception
	 */
	public boolean completePendingCommand(int timeout) throws Exception {
		ftpDaemon.doIt(FTPDaemon.COMPLETE, null);
		if("true".equalsIgnoreCase(getReturnValue("completePendingCommand", timeout))){
			return true;
		}
		return false;
	}
	
	/**
	 * 命令结束(默认超时10秒)
	 * @return
	 * @throws Exception
	 */
	public boolean completePendingCommand() throws Exception {
		return completePendingCommand(1000*30);
	}
	
	/**
	 * 获取FTP操作的返回值
	 * @param operation 操作名称
	 * @param timeout 超时时间(单位:毫秒)
	 * @return 当无返回值时,返回null
	 * @throws Exception
	 */
	private String getReturnValue(String operation, long timeout) throws Exception {
		long startTime = System.currentTimeMillis();
		while(true){
			SysHelper.waitIt(this, 50);
			if(!ftpDaemon.isDoIt()){
				if(ftpDaemon.getException() != null){
					ftpDaemon.interrupt();
					throw ftpDaemon.getException();
				}else{
					return ftpDaemon.getReturnValue();
				}
			}
			
			/** 检查是否超时 */
			if(System.currentTimeMillis()-startTime > timeout){
				ftpDaemon.interrupt();
				throw new Exception("ftp operation[" + operation + "] timeout [" + timeout + "] milliseconds");
			}
		}
	}
	
	/**
	 * 克隆FTP连接
	 * @return
	 * @throws Exception
	 */
	public FTPSrv cloneFTPSrv() throws Exception {
		FTPSrv ftpSrv = new FTPSrv();
		ftpSrv.login(host, port, user, password, encode, timeout);
		if(ftpDaemon.isPassiveMode()){
			ftpSrv.getFtpDaemon().setPassiveMode(true);
		}
		return ftpSrv;
	}
	
	/**
	 * 列出当前工作目录下的所有文件
	 * @param operation
	 * @param timeout
	 * @return
	 * @throws Exception
	 */
	private AFtpRemoteFile[] getFtpRemoteFiles(String operation, long timeout) throws Exception {
		long startTime = System.currentTimeMillis();
		while(true){
			SysHelper.waitIt(this, 50);
			if(!ftpDaemon.isDoIt()){
				if(ftpDaemon.getException() != null){
					ftpDaemon.interrupt();
					throw ftpDaemon.getException();
				}else{
					return ftpDaemon.getFtpRemoteFiles();
				}
			}
			
			/** 检查是否超时 */
			if(System.currentTimeMillis()-startTime > timeout){
				ftpDaemon.interrupt();
				throw new Exception("ftp operation[" + operation + "] timeout [" + timeout + "] milliseconds");
			}
		}
	}
	
	public FTPDaemon getFtpDaemon() {
		return ftpDaemon;
	}


	public RemoteFile[] listRemoteFile(String remoteDir) throws Exception {
		AFtpRemoteFile[] temp = list();
		if (temp == null || temp.length == 0) {
			return new RemoteFile[] {};
		}

		RemoteFile[] file = new RemoteFile[temp.length];
		for (int i = 0; i < temp.length; i++) {
			file[i] = new RemoteFile(temp[i]);
		}

		return file;
	}
	
	public RemoteFile listRemoteFile(String directory, String fileName) throws Exception {
		AFtpRemoteFile[] temp = list();
		if (temp == null || temp.length == 0) {
			return null;
		}

		for (int i = 0; i < temp.length; i++) {
			if(temp[i].getFileName().equals(fileName))
			return new RemoteFile(temp[i]);
		}

		return null;
	}


	public boolean get(String fileName, String localFileName) throws Exception  {
		return retrive(fileName, localFileName);
	}
}