`
liyinfeng56
  • 浏览: 91787 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ftp上传下载

阅读更多
package com.ftp.example;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.FileTransferClient;

public class FtpClientExample {

	/**
	 * 
	 * @param ftpip ftpIP地址
	 * @param user 用户
	 * @param pswd 密码
	 * @return
	 */
	public FileTransferClient getConnect(String ftpip, String user, String pswd) {
		FileTransferClient client = new FileTransferClient();
		try {
			client.setRemoteHost(ftpip);
			client.setUserName(user);
			client.setPassword(pswd);
			client.setRemotePort(Integer.parseInt("21"));
			client.connect();
			if (client.isConnected()) {
				client.setContentType(FTPTransferType.BINARY);
				System.out.println("------ftp连接成功!!");
				client.getAdvancedFTPSettings().setControlEncoding("GB2312");
				return client;
			} else {
				System.out.println("------不能登录FTP服务器,请和管理员联系!!");
				return null;
			}
		} catch (FTPException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 上传到ftp
	 * @param localhostPath 本地的文件路径
	 * @param ftpPath 上传存放文件的ftp路径
	 * @param FileName 存放到ftp文件名
	 * @return
	 * @throws FTPException
	 * @throws IOException
	 * @throws ParseException
	 */
	public boolean uploadFileToFtp(String localhostPath, String ftpPath,
			String FileName) throws FTPException, IOException, ParseException {
		String pathFile = localhostPath;
		String reFile = ftpPath + FileName;
		FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas");
		int length = client.directoryList(ftpPath).length;
		if (length <= 0) {
			System.out.println("FTP文件路径错误");
			return false;
		}
		client.uploadFile(pathFile, reFile);
		client.disconnect();
		return true;
	}

	
	
	/**
	 * 从ftp下载到本地
	 * @param ftpPath
	 * @param fileNames
	 * @param localhostPath
	 * @return
	 * @throws FTPException
	 * @throws IOException
	 * @throws ParseException
	 */
	public boolean downloadFileFromFtp(String ftpPath, List fileNames,
			String localhostPath) throws FTPException, IOException,
			ParseException {
		String pathFile = localhostPath;
		String reFile = ftpPath;
		FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas");
		int length = client.directoryList(ftpPath).length;
		if (length <= 0) {
			System.out.println("ftp路径" + ftpPath + "不是正确:");
			return false;
		}
		System.out.println("可下载的文件个数" + fileNames.size());
		for (int i = 0; i < fileNames.size(); i++) {
			String fileName = (String) fileNames.get(i);
			pathFile = pathFile + "\\" + fileName;
			reFile = reFile + fileName;
			System.out.println(reFile + "是否存在:" + client.exists(reFile));
			if (client.exists(reFile)) {
				System.out.println("pathFile--" + pathFile);
				System.out.println("reFile--" + reFile);
				client.downloadFile(pathFile, reFile);
				client.deleteFile(reFile);
				pathFile = localhostPath;
				reFile = ftpPath;
				continue;
			}
		}
		client.disconnect();
		return true;
	}

	
	
	
	/**
	 * 获取文件夹下面所有的子目录文件
	 * 
	 * @param filePath
	 * @return
	 * @throws ParseException
	 * @throws IOException
	 * @throws FTPException
	 */
	public List getPathFile(String filePath) throws FTPException, IOException,
			ParseException {
		List list = new ArrayList();
		FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas");
		FTPFile diretoryNames[];
		diretoryNames = client.directoryList(filePath);
		if (diretoryNames.length > 0) // 目录下面有文件
		{
			for (int j = 0; j < diretoryNames.length; j++) {
				if (diretoryNames[j].getName().equals(".")
						|| diretoryNames[j].getName().equals(".."))
					continue;
				list.add(diretoryNames[j].getName());
			}
		}
		client.disconnect();
		return list;
	}

	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FtpClientExample example = new FtpClientExample();
		try {
			List list = example.getPathFile("/bank/010/sqzf/");
			System.out.println(list.size());
		} catch (FTPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

分享到:
评论
2 楼 eppen 2011-03-08  
买的企业版?
1 楼 scalong 2010-11-30  
注释太少了

相关推荐

Global site tag (gtag.js) - Google Analytics