使用的是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