Java ftp登录,上传和下载

最近有个项目使用ftp的上传和下载的功能,从网上找了很多有关ftp的内容。我个人觉得使用appache-net比较好。应为这样可以很好的处理中文文件名,一开始我使用sun公司的jar包,但是处理中文不行。以下是ftp的登录和ftp下载代码。

package post.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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;

public class FTPUtil {

	private FTPClient client = null;

	private String remoteDir = "/";

	public boolean login(String host, String user, String pwd)
			throws SocketException, IOException {
		return login(host, user, pwd, "/");
	}

	/**
	 * 登陆
	 * 
	 * @param host
	 * @param user
	 * @param pwd
	 * @return
	 * @throws IOException
	 * @throws SocketException
	 */
	public boolean login(String host, String user, String pwd, String remoteDir)
			throws SocketException, IOException {

		client = new FTPClient();

		client.connect(host);
		client.setControlEncoding("UTF-8");
		int reply = client.getReplyCode();
		client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
		client.setConnectTimeout(0);
		if (!FTPReply.isPositiveCompletion(reply)) {
			client.disconnect();
			System.err.println("FTP server refused connection.");
		}

		this.remoteDir = remoteDir;

		return client.login(user, pwd);

	}

	/***
	 * 得到文件名
	 * 
	 * @return
	 */
	public List<String> getNames(boolean deep) {

		List<String> names = new ArrayList<String>();

		if (client != null) {
			names = checkDirectory(names, client, deep);
		}

		return names;
	}

	public List<FTPFile> getFTPFiles() throws IOException {

		List<FTPFile> list = new ArrayList<FTPFile>();
		if (client != null) {
			FTPFile[] files;
			files = client.listFiles(this.remoteDir);
			for (int i = 0; i < files.length; i++) {
				FTPFile file = files[i];
				String name = file.getName();
				if (name.equals(".") || name.equals("..")) {
					continue;
				} else if (file.isFile()) {
					list.add(file);
				}
			}

		}
		return list;
	}

	private List<String> checkDirectory(List<String> names, FTPClient client,
			boolean deep) {

		try {
			System.out.println("---" + client.printWorkingDirectory());
			FTPFile[] files = client.listFiles(client.printWorkingDirectory());
			for (int i = 0; i < files.length; i++) {
				FTPFile file = files[i];
				// System.out.println(file.getTimestamp().getTime().toLocaleString());
				String name = file.getName();
				if (name.equals(".") || name.equals("..")) {
					continue;
				}

				if (deep && file.isDirectory()) {
					// System.out.println(client
					// .printWorkingDirectory());
					if (client.changeWorkingDirectory(client
							.printWorkingDirectory()
							+ "/" + name)) {
						names = checkDirectory(names, client, deep);
					}
					client.changeToParentDirectory();
				} else if (file.isFile()) {
					String d = client.printWorkingDirectory();
					d = (d.equals("/") ? "" : d);
					name = d + "/" + name;
					names.add(name);
				}

			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return names;
	}

	/***
	 * 下载数据到指定的目录中
	 * 
	 * @param filename
	 *            ftp端文件名称
	 * @param local
	 *            客户端保存路劲
	 * @return
	 */
	public boolean download(String filename, String local) {
		boolean f = false;
		try {
			File file = new File(local
					+ (filename.startsWith("/") ? filename : "/" + filename));
			File file2 = file.getParentFile();
			if (!file2.exists()) {
				file2.mkdirs();
			}
			file2 = null;

			// System.out.println(file.getParent());

			OutputStream out = new FileOutputStream(file);
			f = client.retrieveFile(this.remoteDir + "/" + filename, out);
			out.close();
			// System.out.println(client.getReplyCode());
			client.logout();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (client.isConnected()) {
				try {
					client.disconnect();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return f;
	}

	public static void main(String[] args) {
		FTPUtil ftpUtil = new FTPUtil();
		try {
			boolean c = ftpUtil.login("127.0.0.1", "aata",
					"123");
			System.out.println(c);
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// List<String> list = ftpUtil.getNames(false);
		//
		// for (String string : list) {
		// System.out.println(string);
		// }

		if (ftpUtil.download("20100714.rar", "d:/ftp")) {
			System.out.println("下载成功!");
		}
	}
}

 

 

有时间在把上传的代码贴上。如果大家有更好的,请分享下。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值