使用的是apache开源包commons-net-3.3.jar所提供的FTPClient

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

随机推荐

  1. 格雷码原理与Verilog实现

    格雷码原理 格雷码是一个叫弗兰克*格雷的人在1953年发明的,最初用于通信.格雷码是一种循环二进制码或者叫作反射二进制码.格雷码的特点是从一个数变为相邻的一个数时,只有一个数据位发生跳变,由于这种特点 ...

  2. 大家一起写mvc(二)

    上一篇已经看了,我想大家都明白了mvc的原理,今天我们来说一下要写自己mvc框架必须要会的技术. mvc的目录是这样的 src目录是我们核心的mvc代码.这个代码明天讲,今天主要讲的代码都在test目 ...

  3. mysql php nginx 源码包下载地址

    http://mirror.cogentco.com/pub/mysql/MySQL-5.5/ http://mirrors.sohu.com/php/ http://nginx.org/downlo ...

  4. JSF 2 link, commandLink and outputLink example

    In JSF, <h:link />, <h:commandLink /> and <h:outputLink /> tags are used to render ...

  5. HDU 1251 字典树(前缀树)

    题目大意 :Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).(单词互不相同) ...

  6. jenkins+ant+jmeter测试环境部署

    1.安装java 2.安装jenkins 3.下载apache-jmeter-4.0,解压后放在 /home/用户名/ 下 4.下载apache-ant-1.10.3,解压后放在 /home/用户名/ ...

  7. 安装python--环境配置

    1.下载Python:https://www.python.org/ 2.下载setuptools: https://pypi.Python.org/pypi/setuptools 3.下载pip: ...

  8. Linux 查看网络状态工具

    1. iftop 效果如下图: 界面上面显示的是类似刻度尺的刻度范围,为显示流量图形的长条作标尺用的. 中间的<= =>这两个左右箭头,表示的是流量的方向. TX:发送流量RX:接收流量T ...

  9. 打印星星 - Python

    打印星星是经典面试题目,考察流程控制中的循环和条件.本文对相关方法进行总结. 到的方法只要有(1)嵌套循环(2)center(3)format(^)(4)字符串乘法 # -*- coding:utf- ...

  10. Rserve详解,R语言客户端RSclient【转】

    R语言服务器程序 Rserve详解 http://blog.fens.me/r-rserve-server/ Rserve的R语言客户端RSclient https://blog.csdn.net/u ...