FTP上传下载(Apache Commons Net)

原创 2013年11月04日 17:58:51

使用的是Apache开源包commons-net-3.3.jar,下载地址:http://commons.apache.org/proper/commons-net/download_net.cgi

FTP服务器使用Quick Easy FTP Server 4.0.0(服务器ip为192.168.31.104,端口使用默认21端口,用户名为test,密码为123)

JDK版本为1.6,Junit使用4.8.1

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;

public class FTPutils {
	public static FTPClient getFTPClient(String ip, int port, String uName,
			String uPwd) {
		FTPClient ftpClient = new FTPClient();
		boolean result = true;
		try {
			// use port 21 by default
			// ftpClient.connect(ip);
			// use specific port
			ftpClient.connect(ip, port);
			if (ftpClient.isConnected()) {
				boolean flag = ftpClient.login(uName, uPwd);
				if (flag) {
					ftpClient.setControlEncoding("GBK");
					// binary file
					ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
				} else {
					result = false;
				}
			} else {
				result = false;
			}
			if (result) {
				return ftpClient;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void close(InputStream in, OutputStream out,
			FTPClient ftpClient) {
		if (null != in) {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("Input stream close error!");
			}
		}
		if (null != out) {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("Onput stream close error!");
			}
		}
		if (null != ftpClient) {
			try {
				ftpClient.logout();
				ftpClient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("Ftp client stream close error!");
			}
		}
	}
	
	public static boolean testUpload(String ip, int port, String uName,
			String uPwd, String fileName, String localPath, String remotePath) {
		boolean result = true;
		FileInputStream in = null;
		FTPClient ftpClient = getFTPClient(ip, port, uName, uPwd);
		if (null == ftpClient) {
			System.out.println("Get FTP client failure!");
			return false;
		}
		try {
			File file = new File(localPath + fileName);
			in = new FileInputStream(file);

			ftpClient.changeWorkingDirectory(remotePath);
			ftpClient.storeFile(fileName, in);

			return result;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			close(in, null, ftpClient);
		}
	}

	public static boolean testDownload(String ip, int port, String uName,
			String uPwd, String fileName, String localPath, String remotePath) {
		boolean result = true;
		FileOutputStream out = null;
		FTPClient ftpClient = getFTPClient(ip, port, uName, uPwd);
		if (null == ftpClient) {
			System.out.println("Get FTP client failure!");
			return false;
		}
		try {
			File file = new File(localPath + fileName);
			out = new FileOutputStream(file);

			ftpClient.changeWorkingDirectory(remotePath);
			ftpClient.retrieveFile(fileName, out);
			return result;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			close(null, out, ftpClient);
		}
	}
}

Junit测试类:

test1:上传本地e盘中up.txt文件至FTP服务器根目录下upfile文件夹中

test2:将FTP服务器根目录下downfile文件夹中的down.txt文件下载至本地e盘中

import static org.junit.Assert.*;
import org.junit.Test;

public class FTPutilsTest {
	@Test
	public void testTestUpload() {
		boolean result = FTPutils.testUpload("192.168.31.104", 21, "test", "123", 
				"up.txt", "e:\\", "/upfile");
		assertTrue(result == true);
	}

	@Test
	public void testTestDownload() {
		boolean result = FTPutils.testDownload("192.168.31.104", 21, "test", "123", 
				"down.txt", "e:\\", "/downfile");
		assertTrue(result == true);
	}
}

FTP服务器日志为:

04/11/2013 17:50:34.833 (000004)	 - (not logged in)	(192.168.31.104)>	220 Welcome to LZL's FTP Server V4.0.0
04/11/2013 17:50:34.835 (000004)	 - (not logged in)	(192.168.31.104)>	USER test
04/11/2013 17:50:34.836 (000004)	 - (not logged in)	(192.168.31.104)>	331 Password required for test
04/11/2013 17:50:34.837 (000004)	 - (not logged in)	(192.168.31.104)>	PASS 123
04/11/2013 17:50:34.839 (000004)	 - test	(192.168.31.104)>	230 Client :test successfully logged in. Client IP :192.168.31.104
04/11/2013 17:50:34.840 (000004)	 - test	(192.168.31.104)>	TYPE I
04/11/2013 17:50:34.841 (000004)	 - test	(192.168.31.104)>	200 Type set to I
04/11/2013 17:50:34.843 (000004)	 - test	(192.168.31.104)>	CWD /upfile
04/11/2013 17:50:34.843 (000004)	 - test	(192.168.31.104)>	250 "/upfile" is current directory.
04/11/2013 17:50:34.847 (000004)	 - test	(192.168.31.104)>	PORT 192,168,31,104,255,56
04/11/2013 17:50:34.852 (000004)	 - test	(192.168.31.104)>	200 Port command successful.
04/11/2013 17:50:34.853 (000004)	 - test	(192.168.31.104)>	STOR up.txt
04/11/2013 17:50:34.895 (000004)	 - test	(192.168.31.104)>	150 Opening BINARY mode data connection for file transfer.
04/11/2013 17:50:34.902 (000004)	 - test	(192.168.31.104)>	226 Transfer complete.
04/11/2013 17:50:34.903 (000004)	 - test	(192.168.31.104)>	QUIT
04/11/2013 17:50:34.904 (000004)	 - test	(192.168.31.104)>	220 Bye
04/11/2013 17:50:34.912 (000004)	 - test	(192.168.31.104)>	Client :test disconnected from 192.168.31.104
04/11/2013 17:50:34.920 (000005)	 - (not logged in)	(192.168.31.104)>	220 Welcome to LZL's FTP Server V4.0.0
04/11/2013 17:50:34.921 (000005)	 - (not logged in)	(192.168.31.104)>	USER test
04/11/2013 17:50:34.922 (000005)	 - (not logged in)	(192.168.31.104)>	331 Password required for test
04/11/2013 17:50:34.922 (000005)	 - (not logged in)	(192.168.31.104)>	PASS 123
04/11/2013 17:50:34.932 (000005)	 - test	(192.168.31.104)>	230 Client :test successfully logged in. Client IP :192.168.31.104
04/11/2013 17:50:34.948 (000005)	 - test	(192.168.31.104)>	TYPE I
04/11/2013 17:50:34.958 (000005)	 - test	(192.168.31.104)>	200 Type set to I
04/11/2013 17:50:34.974 (000005)	 - test	(192.168.31.104)>	CWD /downfile
04/11/2013 17:50:34.984 (000005)	 - test	(192.168.31.104)>	250 "/downfile" is current directory.
04/11/2013 17:50:34.994 (000005)	 - test	(192.168.31.104)>	PORT 192,168,31,104,255,59
04/11/2013 17:50:35.008 (000005)	 - test	(192.168.31.104)>	200 Port command successful.
04/11/2013 17:50:35.016 (000005)	 - test	(192.168.31.104)>	RETR down.txt
04/11/2013 17:50:35.026 (000005)	 - test	(192.168.31.104)>	150 Opening BINARY mode data connection for file transfer.
04/11/2013 17:50:35.041 (000005)	 - test	(192.168.31.104)>	226 Transfer complete.
04/11/2013 17:50:35.050 (000005)	 - test	(192.168.31.104)>	QUIT
04/11/2013 17:50:35.090 (000005)	 - test	(192.168.31.104)>	220 Bye
04/11/2013 17:50:35.116 (000005)	 - test	(192.168.31.104)>	Client :test disconnected from 192.168.31.104

版权声明:本文为博主原创文章,未经博主允许不得转载。

使用Apache Commons Net包中的FTPClient实现javaFTP来上传目录(新)

package com.util;      import java.io.*;   import java.net.SocketException;   import java.text.S...
  • gaoqiao1988
  • gaoqiao1988
  • 2016年10月24日 13:47
  • 4539

Android中使用Apache common ftp进行下载文件

Android中使用Apache common ftp进行下载文件
  • birdsaction
  • birdsaction
  • 2014年07月02日 10:23
  • 9751

Java中apache下面FTPClient主动模式和被动模式

分享学习,希望对以后碰到的人有帮助
  • caolipeng_918
  • caolipeng_918
  • 2015年12月22日 10:24
  • 9140

apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出

apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出 标签: ftp服务器stringfileexception服务器null 2012-05-15 16:...
  • gaoqiao1988
  • gaoqiao1988
  • 2016年10月24日 11:24
  • 759

apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出

用到了apache 的 commons-net-3.0.1.jar 和 log4j-1.2.15.jar 这连个jar包 package com.bjut.edu.cn.ft...
  • changqingming
  • changqingming
  • 2015年05月20日 16:51
  • 332

apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出

用到了apache 的 commons-net-3.0.1.jar 和 log4j-1.2.15.jar 这连个jar包 JAVA 代码如下: package com.bjut.edu.cn.ft...
  • duanduanlong
  • duanduanlong
  • 2012年05月15日 16:29
  • 17073

使用Apache Commons Net API实现FTP上传下载过程中的坑点

最近项目需要实现FTP上传、下载功能,采用了Apache Commons Net API。代码很快就完成了,但由于对相关API使用场景不是很熟悉,走了一些弯路,抽一点时间做一下总结。 A)主动被动模式...
  • huanggang028
  • huanggang028
  • 2014年11月24日 16:17
  • 5518

apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出

原文地址:http://blog.csdn.net/duanduanlong/article/details/7569324 用到了apache 的 commons-net-3.0....
  • xiyang_1990
  • xiyang_1990
  • 2016年10月31日 16:42
  • 377

Android开发 之 利用commons-net-3.0.1.jar包实现FTP上传下载功能

原文链接地址: http://blog.csdn.net/ljphhj/article/details/11859593#comments 感谢作者:胖虎 无私的提供Demo,以及其在我实现过程中的指...
  • liuyixin01
  • liuyixin01
  • 2013年10月16日 15:44
  • 2247

Android 利用commons-net-3.3实现ftp上传下载

android上实现ftp文件上传下载等功能。
  • a512337862
  • a512337862
  • 2017年05月22日 13:20
  • 548
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:FTP上传下载(Apache Commons Net)
举报原因:
原因补充:

(最多只允许输入30个字)