FTP 工具类. 可以完成对目录创建的创建、修改、删除,对文件的上传下载等操作

package rewin.util;

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 java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
 * FTP 工具类. 可以完成对目录创建的创建、修改、删除,对文件的上传下载等操作.
 * @author yuyoufa
 * @since 1.4
 */
public class FtpUtil {
	
	/**
	 * FTP客户端
	 */
	private FTPClient ftp;
	public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
	public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
	
	/**
	 * 初始化客户端并完成对服务端的连接
	 * @param ftpConfig ftp配置类
	 * @throws SocketException
	 * @throws IOException
	 */
	public void connectServer(FtpConfig ftpConfig) throws SocketException,
			IOException {
		String server = ftpConfig.getServer();
		int port = ftpConfig.getPort();
		String user = ftpConfig.getUsername();
		String password = ftpConfig.getPassword();
		String location = ftpConfig.getLocation();
		connectServer(server, port, user, password, location);
	}
	
	/**
	 * 初始化客户端并完成对服务端的连接
	 * @param server 服务端地址
	 * @param port 端口号
	 * @param username 用户名
	 * @param password 密码
	 * @param path 远程路径 值可以为空
	 * @throws SocketException
	 * @throws IOException
	 */
	public void connectServer(String server, int port, String username,
			String password, String path) throws SocketException, IOException {
		ftp = new FTPClient();
		ftp.connect(server, port);
		ftp.setDataTimeout(120000);
		if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {   
			ftp.disconnect();   
            System.out.println(server + " 拒绝连接");   
        }
		ftp.login(username, password);
		if (path.length() != 0) {
			ftp.changeWorkingDirectory(path);
		}
	}
	
	/**
	 * 设置ftp的文件传输类型
	 * @param fileType 如:FTP.BINARY_FILE_TYPE
	 * @throws IOException
	 */
	public void setFileType(int fileType) throws IOException {
		ftp.setFileType(fileType);
	}
	
	/**
	 * 关闭ftp连接
	 * @throws IOException
	 */
	public void closeServer() throws IOException {
		if (ftp != null && ftp.isConnected()) {
			ftp.logout();
			ftp.disconnect();
		}
	}
	
	/**
	 * 改变ftp的工作目录
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public boolean changeDirectory(String path) throws IOException {
		return ftp.changeWorkingDirectory(path);
	}
	
	/**
	 * 在服务端创建目录
	 * @param pathName 可以是相对目录或绝对目录
	 * @return
	 * @throws IOException
	 */
	public boolean createDirectory(String pathName) throws IOException {
		return ftp.makeDirectory(pathName);
	}
	
	/**
	 * 删除一个FTP服务器上的目录(如果为空)
	 * @param path 目录路径
	 * @return
	 * @throws IOException
	 */
	public boolean removeDirectory(String path) throws IOException {
		return ftp.removeDirectory(path);
	}

	/**
	 * 删除一个FTP服务器上的目录
	 * @param path 目录路径
	 * @param isAll 是否删除所有子目录和文件,如果有
	 * @return
	 * @throws IOException
	 */
	public boolean removeDirectory(String path, boolean isAll)
			throws IOException {

		if (!isAll) {
			return removeDirectory(path);
		}
		//遍历子目录和文件
		FTPFile[] ftpFileArr = ftp.listFiles(path);
		if (ftpFileArr == null || ftpFileArr.length == 0) {
			return removeDirectory(path);
		}

		for (int i = 0; i < ftpFileArr.length; i++) {
			FTPFile ftpFile = ftpFileArr[i];
			String name = ftpFile.getName();
			if (ftpFile.isDirectory()) {
				removeDirectory(path + "/" + name, true);
			} else if (ftpFile.isFile()) {
				deleteFile(path + "/" + name);
			} else if (ftpFile.isSymbolicLink()) {
				
			} else if (ftpFile.isUnknown()) {
				
			}
		}
		return removeDirectory(path);
	}
	
	/**
	 * 返回给定目录下的文件
	 * @param path
	 * @return FTPFile组成的集合
	 * @throws IOException
	 */
	public List getFileList(String path) throws IOException {

		FTPFile[] ftpFiles = ftp.listFiles(path);

		List retList = new ArrayList();
		if (ftpFiles == null || ftpFiles.length == 0) {
			return retList;
		}
		for (int i = 0; i < ftpFiles.length; i++) {
			FTPFile ftpFile = ftpFiles[i];
			if (ftpFile.isFile()) {
				retList.add(ftpFile.getName());
			}
		}
		return retList;
	}
	
	/**
	 * 删除文件
	 * @param pathName 文件名
	 * @return 删除结果,是否成功.
	 * @throws IOException
	 */
	public boolean deleteFile(String pathName) throws IOException {
		return ftp.deleteFile(pathName);
	}
	
	/**
	 * 上传文件,并重命名.
	 * @param fileName 上传的文件,包含目录的文件名
	 * @param newName 新的文件名
	 * @return 上传结果,是否成功.
	 * @throws IOException
	 */
	public boolean uploadFile(String fileName, String newName)
			throws IOException {
		boolean flag = false;
		InputStream iStream = null;
		try {
			iStream = new FileInputStream(fileName);
			flag = ftp.storeFile(newName, iStream);
		} catch (IOException e) {
			flag = false;
			return flag;
		} finally {
			if (iStream != null) {
				iStream.close();
			}
		}
		return flag;
	}
	
	/**
	 * 上传文件
	 * @param fileName 上传的文件,包含目录的文件名
	 * @return 上传结果,是否成功.
	 * @throws IOException
	 */
	public boolean uploadFile(String fileName) throws IOException {
		return uploadFile(fileName, fileName);
	}
	
	/**
	 * 上传文件,从InputStream
	 * @param iStream 文件流
	 * @param newName 新的文件名
	 * @return 上传结果,是否成功.
	 * @throws IOException
	 */
	public boolean uploadFile(InputStream iStream, String newName)
			throws IOException {
		boolean flag = false;
		try {
			flag = ftp.storeFile(newName, iStream);
		} catch (IOException e) {
			flag = false;
			return flag;
		} finally {
			if (iStream != null) {
				iStream.close();
			}
		}
		return flag;
	}
	
	/**
	 * 下载文件
	 * @param remoteFileName 远程文件名
	 * @param localFileName 本地文件
	 * @return 返回操作结果
	 * @throws IOException
	 */
	public boolean download(String remoteFileName, String localFileName)
			throws IOException {
		boolean flag = false;
		File outfile = new File(localFileName);
		OutputStream oStream = null;
		try {
			oStream = new FileOutputStream(outfile);
			flag = ftp.retrieveFile(remoteFileName, oStream);
		} catch (IOException e) {
			flag = false;
			return flag;
		} finally {
			oStream.close();
		}
		return flag;
	}
	
	/**
	 * 下载文件,返回InputStream
	 * @param sourceFileName 远程文件
	 * @return InputStream
	 * @throws IOException
	 */
	public InputStream downFile(String sourceFileName) throws IOException {
		return ftp.retrieveFileStream(sourceFileName);
	}

	public FTPClient getFtp() {
		return ftp;
	}

	public void setFtp(FTPClient ftp) {
		this.ftp = ftp;
	}

}

你可能感兴趣的:(JAVA)