⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpuploadthread.java

📁 eclipse平台的CDT项目3.0版本的源代码
💻 JAVA
字号:
package cdt.projects.export.ftp;

import cdt.projects.export.ftp.FtpWizard2;

import com.ibm.network.ftp.protocol.*;
import com.ibm.network.ftp.event.*;

import java.io.*;

/**
 * This is a class that uploads the files.  It's in its own class so we can multi-thread it.
 *
 * @author David Bradford
 *		   Brad Schaefer (<A HREF="mailto:schaefer@medstat.med.utah.edu">schaefer@medstat.med.utah.edu</A>)
 */
public class FtpUploadThread implements Runnable, StatusListener {
	/** directory that has the files to upload. */
	private static File tempDir;
	/** ftp stuff to upload with. */
	private FTPProtocol ftp;
	/** ftp wizard page so we can tell it when we're done. */
	private FtpWizard2 wiz;
	/** boolean that indicates whether the thread needs to be stopped or not. */
	private boolean endThread;
	/** the number of the file we're currently uploading. */
	private int currentFileNumber;
	/** the <code>String</code> representing the file we're currently uploading. */
	private String currentFileName;

	/**
	 * Creates an instance of Upload.
	 *
	 * @param tempDir directory where file to upload are.
	 * @param ftp stuff to upload with.
	 * @param wiz page to tell when we're done.
	 */
	public FtpUploadThread(File tempDir, FTPProtocol ftp, FtpWizard2 wiz) {
		this.tempDir = tempDir;
		this.ftp = ftp;
		this.wiz = wiz;
		this.endThread = false;
		this.currentFileNumber = 0;
		this.currentFileName = new String();
		ftp.addStatusListener(this);
	}

	/**
	 * Start sending the files.
	 */
	public void run() {
		sendFiles(tempDir);
		if(!endThread) {
			wiz.FtpDone();
		} else {
			wiz.FtpCancelled();
		}
	}

	/**
	 * Doesn't do anything must be needed to implement StatusListener.
     *
     * @param event Not used.
	 */
	public void statusReceived(StatusEvent event) {	}
	
	/**
	 * Lets the FTP thread know that the upload was closed in the
	 * {@link cdt.projects.export.ftp.FtpWizard2 wizard}, so end the thread.
	 */
	public void notifyFail() {
		endThread = true;
	}

	/**
	 * Sends all the files in a directory to ftp.
	 *
	 * @param dir directory containing files to be sent.
	 */
	private void sendFiles(File dir) {
		File[] files = dir.listFiles();
		for(int i=0;i<files.length;i++) {
			if(this.endThread) { break; }
			if(files[i].isDirectory()) {
				sendDir(files[i]);
			} else {
				this.currentFileName = files[i].getName();
				++currentFileNumber;
				ftp.putFile(currentFileName);
			}
		}
	}

	/**
	 * sends a directory and all the files in it.
	 *
	 * @param dir directory to be sent.
	 */
	private void sendDir(File dir) {
		ftp.makeDir(dir.getName(), true);
		ftp.changeDir(dir.getName(), true);
		ftp.changeDir(dir.getName(), false);
		sendFiles(dir);
		ftp.changeDir("..", false);
		ftp.changeDir("..", true);
	}
	
	/**
	 * @return The number of the file we're currently uploading.
	 */
	public int getCurrentFileNumber() {
		return this.currentFileNumber;
	}
	
	/**
	 * @return The name of the file we're currently uploading.
	 */
	public String getCurrentFileName() {
		return this.currentFileName;
	}
	
	/**
	 * Counts the number of files in the directory recursively.  A bit slow, so
	 * we don't want to be calling this a lot.
	 *
     * @param dir The base directory to count the files from.
	 * @return The number of files that will be uploaded.
	 */
	private static int countFiles(File dir) {
		File[] files = dir.listFiles();
		int ret = 0;
		for(int i = 0; i < files.length; ++i) {
			if(files[i].isDirectory()) {
				ret += countFiles(files[i]);
			} else {
				++ret;
			}
		}
		return ret;
	}
	
	/**
	 * Counts the total number of files that are in the C:\temp\cdtTemp\ directory.
	 *
	 * @return The number of files (not including directories) in the C:\temp\cdtTemp\ directory.
	 * @see cdt.projects.export.ftp.FtpWizard2
	 */
	public static int getTotalFiles() {
		return countFiles(tempDir);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -