实现通过common-net.jar 并用junit测试
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @version 创建时间:2015年12月14日 上午9:45:32
*
*/
public class JunitFtp {
private static FTPClient ftpClient;
private static String ftpIp = "192.168.0.229";
private static String ftpUser = "ff";
private static String ftpPassWord = "ff";
private static String ftpPort = "21";
private static String workingDirectory = "\\data";
private static String localDirectory = "E:\\home\\ftp\\download";
@BeforeClass
public static void setUpBeforeClass() throws Exception {
// ftp连接
ftpClient = new FTPClient();
ftpClient.setControlEncoding("gb2312");
ftpClient.connect(ftpIp, Integer.parseInt(ftpPort));
ftpClient.login(ftpUser, ftpPassWord);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
ftpClient.disconnect();
}
/**
*单个文件下载
* @throws IOException
* "JiaYanFei"
* 2015年12月14日
*/
@Test
public void fileDownload() throws IOException {
// ftpClient.setControlEncoding("gb2312");
FileOutputStream fos = null;
try {
ftpClient.connect(ftpIp, Integer.parseInt(ftpPort));
ftpClient.login(ftpUser, ftpPassWord);
ftpClient.changeWorkingDirectory(workingDirectory);
System.out.println("dir:" + ftpClient.printWorkingDirectory());
FTPFile[] files = ftpClient.listFiles();
// FTP服务器文件路径
String remoteFileName = "\\data\\中.txt";// 已经切换到data目录只写 中.txt 也可以 但是 写\\中.txt不行
String newFileName = "文.txt";
// 文件下载存放路径
fos = new FileOutputStream(localDirectory + File.separator+ newFileName);
fos.flush();
ftpClient.setBufferSize(1024);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
if (ftpClient.retrieveFile(remoteFileName, fos)) {
System.out.println(" 文件下载成功。。");
} else {
System.out.println(" 文件下载失败。。");
fail("下载失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(fos);
ftpClient.disconnect();
}
}
@Test
public void fileUpload() throws IOException {
FileInputStream fis = null;
try {
ftpClient.connect(ftpIp, Integer.parseInt(ftpPort));
ftpClient.login(ftpUser, ftpPassWord);
ftpClient.changeWorkingDirectory("/data");
fis = new FileInputStream(new File("E:\\home\\ftp\\data\\o.txt"));
if (ftpClient.storeFile("/data/backFile3.txt", fis)) {// /backFile3.txt backFile3.txt也可以
System.out.println("上传成功!");
} else {
System.out.println("上传失败");
fail("上传失败");
}
} finally {
IOUtils.closeQuietly(fis);
ftpClient.disconnect();
}
}
/**
*目录下载
* @throws IOException
* 2015年12月14日
*/
@Test
public void downloadDirectory() throws IOException {
downloadDirectory("\\data" ,localDirectory+ "\\data");
}
/**
*目录递归下载
* @param basePath
* @param localPath
* @throws IOException
* 2015年12月14日
*/
public void downloadDirectory(String basePath, String localPath) throws IOException{
ftpClient.changeWorkingDirectory(basePath);
System.out.println(ftpClient.printWorkingDirectory());
FTPFile[] files = ftpClient.listFiles();
if (0 == files.length) {// 目录为空 创建空目录
System.out.println("dir name: " +localPath );
new File(localPath ).mkdirs();
return;
}
File file = null;
for(FTPFile f:files){
if(f.isDirectory()){
downloadDirectory(basePath + File.separator + f.getName(), localPath + File.separator + f.getName());
}else{
file = new File(localPath);//先判断本地目录是否存在 --不存在则创建
if(!file.exists()){
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream( localPath + File.separator + f.getName());
fos.flush();
ftpClient.setBufferSize(1024);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
System.out.println("fileName: " + f.getName());
if (ftpClient.retrieveFile(basePath + File.separator +f.getName(), fos)) {
System.out.println(" 文件下载成功。。");
} else {
System.out.println(" 文件下载失败。。");
fail("下载失败");
}
}
}
}
}