All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspire.nm.component.commonUtil.ftp.FtpUtil Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
/**
 * 
 */

package com.aspire.nm.component.commonUtil.ftp;

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 org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;


public class FtpUtil {
	
	private static Logger logger = Logger.getLogger(FtpUtil.class);
	
	/**
	 * 操作成功
	 */
	public final static int OPERATE_SUCCEED = 0;
	/**
	 * 操作失败
	 */
	public final static int OPERATE_FAIL = 1;

	
	private static FTPClient fClient;
	
	
	

	/**
	 * 与ftp服务器建立连接
	 * 
	 * @hostName  主机IP
	 * @userName  用户名
	 * @passWord  密码
	 * @timeOut  超时时间
	 * @return int FTPUtils.OPERATE_SUCCEED 或者 FTPUtils.OPERATE_FAIL
	 */
	public static int connect(String hostName, String userName, String passWord, int timeOut) {
		fClient = new FTPClient();

		try {
			fClient.connect(hostName);
			fClient.login(userName, passWord);
			fClient.setSoTimeout(timeOut);
			fClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
			fClient.setFileType(FTPClient.BINARY_FILE_TYPE);

			//fClient.enterLocalActiveMode();
			fClient.enterLocalPassiveMode();
			return OPERATE_SUCCEED;
		} catch (IOException ex) {
			logger.error(ex);
			return OPERATE_FAIL;
		}
	}

	/**
	 * 本地指定路径的文件上传到服务器上
	 * 
	 * @remotePath 服务器上文件路径
	 * @localPath 本地路径
	 * @hostName  主机IP
	 * @userName  用户名
	 * @passWord  密码
	 * @timeOut  超时时间
	 * @return int FTPUtils.OPERATE_SUCCEED 或者 FTPUtils.OPERATE_FAIL
	 */
	public static int upLocalFileToFTP(String remotePath, String localPath,
		String hostName, String userName, String passWord, int timeOut){
		int ret = threeCon(hostName, userName, passWord, timeOut);
		if (OPERATE_FAIL == ret) {
			return OPERATE_FAIL;
		}

		InputStream is = null;
		try {
			is = new FileInputStream(localPath);
			if (fClient.storeFile(remotePath, is)) {
				return OPERATE_SUCCEED;
			}
		}catch(Exception e){
			logger.error(e);
		}finally {
			if (null != is) {
				try{
					is.close();
				}
				catch(Exception e){
					logger.error(e);
				}
			}
		}
		return OPERATE_FAIL;
	}

	/**
	 * 将服务器上指路径的文件下载到本地
	 * 
	 * @remotePath 服务器上文件路径
	 * @localPath 本地路径
	 * @hostName  主机IP
	 * @userName  用户名
	 * @passWord  密码
	 * @timeOut  超时时间
	 * @return int FTPUtils.OPERATE_SUCCEED 或者 FTPUtils.OPERATE_FAIL
	 */
	public static int downFtpFileToLoad(String remotePath, String localPath,
			String hostName, String userName, String passWord, int timeOut)
	{
		// 如果连接断了,自动重连
		int ret = threeCon(hostName, userName, passWord, timeOut);
		if (OPERATE_FAIL == ret) {
			return OPERATE_FAIL;
		}
		OutputStream os = null;
		try {
			// 如果保存在本地的文件不存在则自动创建
			File file = new File(localPath);
			File dirs = file.getParentFile();
			if (!dirs.exists()) {
				dirs.mkdirs();
			}
			os = new FileOutputStream(file);
			// 判断ftp服务器上的文件下载到本地是否成功
			if (fClient.retrieveFile(remotePath, os)) {
				return OPERATE_SUCCEED;
			}
		}catch(Exception e){
			logger.error(e);
		}finally {
			if (null != os) {
				try{
					os.flush();
					os.close();
				}catch(Exception e){
					logger.error(e);
				}
			}
		}
		return OPERATE_FAIL;
	}

	/**
	 * 删除服务器上指定路径的文件
	 * 
	 * @remotePath 服务器上文件路径
	 * @hostName  主机IP
	 * @userName  用户名
	 * @passWord  密码
	 * @timeOut  超时时间
	 * @return int FTPUtils.OPERATE_SUCCEED 或者 FTPUtils.OPERATE_FAIL
	 */
	public static int deleteFtpFile(String remotePath, String hostName,
			String userName, String passWord, int timeOut)
	{
		int ret = threeCon(hostName, userName, passWord, timeOut);
		if (OPERATE_FAIL == ret) {
			return OPERATE_FAIL;
		}
		try{
			if (fClient.deleteFile(remotePath)) {
				return OPERATE_SUCCEED;
			}
		}catch(Exception e){
			logger.error(e);
			return OPERATE_FAIL;
		}
		return OPERATE_FAIL;
	}

	/**
	 * 获取服务器上指定路径的文件名称列表
	 * 
	 * @remotePath 服务器上文件路径
	 * @hostName  主机IP
	 * @userName  用户名
	 * @passWord  密码
	 * @timeOut  超时时间
	 * 
	 * @return String[] 文件名称列表
	 */
	public static String[] getFileNameList(String remotePath, String hostName,
			String userName, String passWord, int timeOut){
		String[] fileNames = null;
		int ret = threeCon(hostName, userName, passWord, timeOut);
		if (OPERATE_FAIL == ret) {
			return fileNames;
		}
		try{
			fileNames = fClient.listNames(remotePath);
		}catch(Exception e){
			logger.error(e);
		}
		return fileNames;
	}

	/**
	 * FTP上创建目录
	 * @pathName 创建目录路径
	 */
	public static void makeDirectory(String pathName) {
		if (null != pathName && !"".equals(pathName))
		{
			try{
				if (!fClient.makeDirectory(pathName))
				{
					String[] strs = pathName.split("/");
					String temp = "";
					for (int i = 0; i < strs.length; i++)
					{
						temp = temp + strs[i] + "/";
						if (fClient.makeDirectory(temp))
						{
						}
					}
				}
			}catch(Exception e){
				logger.error(e);
			}
		}
	}

	/**
	 * 关闭FTP连接
	 * 
	 */
	public static void closeFTP() {
		if (null != fClient) {
			try {
				fClient.logout();
			} catch (IOException ex) {
				logger.error(ex);
			} finally {
				try {
					fClient.disconnect();
				} catch (IOException e) {
					logger.error(e);
				}
			}
		}
	}

	/**
	 * 远程移动ftp上的文件
	 * 
	 * @from 源目录
	 * @to 目的目录
	 * @hostName  主机IP
	 * @userName  用户名
	 * @passWord  密码
	 * @timeOut  超时时间
	 * @return int FTPUtils.OPERATE_SUCCEED 或者 FTPUtils.OPERATE_FAIL
	 */
	public static boolean moveRemoteFile(String from, String to, String hostName,
			String userName, String passWord, int timeOut)throws IOException {
		boolean re = false;
		int ret = threeCon(hostName, userName, passWord, timeOut);
		if (OPERATE_FAIL == ret)
		{
			return false;
		}
		re = fClient.rename(from, to);
		return re;
	}
	
	
	
	private static int threeCon(String hostName, String userName, String passWord, int timeOut) {
		if (null != fClient) {
			return OPERATE_SUCCEED;
		}
		for (int i = 0; i < 3; i++) {
			int inConn = connect(hostName, userName, passWord, timeOut);
			if (OPERATE_FAIL == inConn) {
				continue;
			}

			return OPERATE_SUCCEED;
		}

		return OPERATE_FAIL;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy