`

ftp工具类

 
阅读更多
每回用到总去网上找一通,还是自已总结下比较好
package com.hjb.transForm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Vector;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
/**
 * 
 *  @Class       : FtpUtil
 *  @Description : ftp工具
 *  @since       :  v2.0
 *  @ModificationHistory  
 *  Who        When          What 
 *  -------   ---------    -----------------------------------
 *  zhangshoulei    2014年9月19日      创建
 *
 *  @version : TODO 最后修改时项目版本
 */
public class FtpUtil {
	public static FTPClient getFtpClient(String address, int port, String uname, String pwd, String encoding)
			throws Exception {
		FTPClient client = null;

		client = new FTPClient();
		client.connect(address, port);
		boolean islogin = client.login(uname, pwd);
		if (!islogin) {
			throw new Exception("帐号或者密码错误");
		}
		//client.setControlEncoding(encoding);

		return client;
	}

	/**
	 * FTP文件递归 
	 * @throws Exception 
	 * */
	public static List<String> ftpRecursive(FTPClient client, String dir, String localDir) throws Exception {
		List<String> fileNames = new Vector<String>();
		FTPFile[] ftpFiles = client.listFiles(dir);
		for (FTPFile file : ftpFiles) {
			String name = dir + "/" + file.getName();
			if (file.isDirectory()) {
				ftpRecursive(client, dir, name);
			} else {
				File localFile = new File(localDir + name);
				if (!localFile.exists()) {
					downFile(client, name, localFile);
				}
			}
		}
		return fileNames;
	}

	public static void downFile(FTPClient client, String fileName, File localFile) {
		try {
			System.out.println("正在下载文件:" + fileName);
			localFile.getParentFile().mkdirs();
			InputStream is = client.retrieveFileStream(fileName);
			if (is == null) {
				System.out.println("下载失败" + fileName);
				return;
			}
			OutputStream os = new FileOutputStream(localFile);
			int len = -1;
			byte[] buffer = new byte[1024];
			int all = 0;
			while ((len = is.read(buffer)) != -1) {
				all += len;
				System.out.println("下载:" + all);
				os.write(buffer, 0, len);
			}
			os.flush();
			os.close();
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**javaftp上传*/
	public static boolean upload(FTPClient client, String localFile, String remoteName) {
		try {
			File file = new File(localFile);
			if (file.exists()) {
				InputStream local = new FileInputStream(file);

				return client.appendFile(remoteName, local);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
/** 
	 * 断开ftp连接 
	 *  
	 * @throws 
	 */
	public static void disconnect(FTPClient ftpClient) {
		try {
			if (ftpClient != null) {
				ftpClient.logout();
				if (ftpClient.isConnected()) {
					ftpClient.disconnect();
					ftpClient = null;
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
	}

	public static void main(String[] args) throws Exception {
		FTPClient client = FtpUtil.getFtpClient("192.168.39.89", 21, "admin", "123", "UTF-8");
		System.out.println(FtpUtil.upload(client, "E:/HJB01_file_index_20140723_1.zip", "/aa.zip"));
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics