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

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

相关文章推荐

ftp与sftp文件上传工具类

1、ftp上传工具类 package com.deppon.logistics.utils; import java.io.IOException; import java.io.InputStr...

Java FTP工具类

因工作需要,需要在后台处理一些小文件,比如网页资源到服务器上保存,本来计划使用DFS来进行压缩存储,考虑到需要再次解压生成访问连接,暂时先直接通过FTP直接保存在服务器上。就看了下Java下的ftp上...

FTP 上精确取文件,提供byte[]传出,再转本地文件

import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import...

最经典的Java FTP操作类

今天给大家带来自己写的ftp操作类,主要用于java网站、桌面程序的文件上传,图片上传以及压缩处理。本文中需要用到commons-net-2.2.jar   thumbnailator-0.4.8.j...

java使用Apache工具集实现ftp文件传输

本文主要介绍如何使用Apache工具集commons-net提供的ftp工具实现向ftp服务器上传和下载文件。
  • zleven
  • zleven
  • 2016-12-26 15:50
  • 1066

JAVA中使用FTPClient实现FTP上文件的操作

Java通过Apache的工具类

JAVA学习(一)__操作FTP

//FFP初始化连接 public static void init(){ Properties config = new Properties(); try{ System.out....

Java语言实现简单FTP软件------>FTP软件主界面的实现(四)

Java语言实现简单FTP软件------>FTP软件主界面的实现(四)    首先介绍程序的主入口FTPMain.java,采用了一个漂亮的外观风格。

JDK1.7 ftpclient的方法示例

程序升级JDK7发现ftp下载存在与JDK6不兼容的问题,看了下网上的示例,没有找到API接口文档,自己凭借不是很好的英文看了下接口API, 提供一下示例,有问题请指出 package com.u...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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