当前位置 : 主页 > 编程语言 > java >

ftp文件服务器上传/下载

来源:互联网 收集:自由互联 发布时间:2021-06-28
FtpClientService package com.skylink.eos.common;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.SocketExcep
FtpClientService
package com.skylink.eos.common;

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 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 atimin
 * 
 */
public class FtpClientService {

	/**
	 * 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 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 logout() 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 ArrayList fileNames(String path) {
		ArrayList retList = new ArrayList();
		try {
			FTPFile[] ftpFiles = ftp.listFiles(path);
			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());
				}
			}
		} catch (Exception ex) {
		}
		return retList;
	}

	/**
	 * 读取目录列表
	 * 
	 * @param path
	 * @return
	 */
	public ArrayList listFiles(String path) {
		ArrayList retList = new ArrayList();
		try {
			FTPFile[] ftpFiles = ftp.listFiles(path);
			if (ftpFiles == null || ftpFiles.length == 0) {
				return retList;
			}
			for (int i = 0; i < ftpFiles.length; i++) {
				FTPFile ftpFile = ftpFiles[i];
				if (ftpFile.isDirectory()) {
					retList.add(ftpFile.getName());
				}
			}
		} catch (Exception ex) {
		}
		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 upFile(String fileName, String newName) throws IOException {
		boolean flag = false;
		InputStream iStream = null;
		try {
			iStream = new FileInputStream(fileName);
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			flag = ftp.storeFile(newName, iStream);
		} catch (IOException e) {
			flag = false;
			return flag;
		} finally {
			if (iStream != null) {
				iStream.close();
			}
		}
		return flag;
	}

	/**
	 * 上传文件,从InputStream
	 * 
	 * @param iStream
	 *            文件流
	 * @param newName
	 *            新的文件名
	 * @return 上传结果,是否成功.
	 * @throws IOException
	 */
	public boolean upFile(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 downFile(String remoteFileName, String localFileName) {
		boolean flag = false;
		try {
			File outfile = new File(localFileName);
			OutputStream oStream = null;
			try {
				oStream = new FileOutputStream(outfile);
				ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
				flag = ftp.retrieveFile(remoteFileName, oStream);
			} catch (IOException e) {
				flag = false;
				return flag;
			} finally {
				oStream.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return flag;
	}

	/**
	 * 移动FTP文件
	 * 
	 * @param remoteSrc
	 * @param remoteDst
	 * @return
	 */
	public boolean moveRemoteFile(String remoteSrc, String remoteDst) {
		boolean flag = false;
		try {
			try {
				ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
				flag = ftp.rename(remoteSrc, remoteDst);
			} catch (IOException e) {
				flag = false;
				return flag;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return flag;
	}

	public FTPClient getFtp() {
		return ftp;
	}

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

}
网友评论