使用apache commons net进行ftp传输

apache commons net的maven地址:
http://mvnrepository.com/artifact/commons-net/commons-net/3.6

<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.6</version>
</dependency>

apache commons net的说明文档地址:http://commons.apache.org/proper/commons-net/

apache commons net的依赖关系:http://commons.apache.org/proper/commons-net/dependencies.html
看到apache commons net 3.6依赖于junit 4.12。

下面的测试程序用于下载指定位置的ftp上的文件到本地目录:

package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class FTPDataGetter {
	public static final String FTP_SRV_HOSTNAME = "192.168.100.35";
	public static final Integer FTP_SRV_PORT = 21;
	public static final String FTP_SRV_USERNAME = "zifeiy";
	public static final String FTP_SRV_PASSWORD = "password";
	public static final String LOCAL_DIRECTORY = "E:\\report";
	
	private FTPClient ftpClient = null;
	
	public FTPDataGetter() {
		this.ftpClient = new FTPClient();
		ftpClient.setControlEncoding("utf-8");
		try {
			ftpClient.connect(FTP_SRV_HOSTNAME, FTP_SRV_PORT);
			ftpClient.login(FTP_SRV_USERNAME, FTP_SRV_PASSWORD);
			int replyCode = ftpClient.getReplyCode();
			if (FTPReply.isPositiveCompletion(replyCode) == true) {
				// 登陆成功
				System.out.println("FTP connect to " + FTP_SRV_HOSTNAME + ":" + FTP_SRV_PORT + " succeed!");
			}
			else {
				System.out.println("FTP connect to " + FTP_SRV_HOSTNAME + ":" + FTP_SRV_PORT + " failed!");
				this.ftpClient = null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void downloadOneDayFiles(String dayString) {
		if (this.ftpClient == null) {
			System.out.println("[ERROR] because ftp client is NULL!");
			return;
		}
		try {
			ftpClient.changeWorkingDirectory("sdata/S-999000");
			// 进入系统文件夹
			for (FTPFile sysFile : this.ftpClient.listDirectories()) {
				String sysName = sysFile.getName();
//				System.out.println("sysName = " + sysName);
				boolean changeSysFlag = ftpClient.changeWorkingDirectory(sysName);
				if (changeSysFlag == true) {
					// 进入类型文件夹 ADD或者ALL
					for (FTPFile typeFile : this.ftpClient.listDirectories()) {
						String typeName = typeFile.getName();
						boolean changeTypeFlag = ftpClient.changeWorkingDirectory(typeName);
						if (changeTypeFlag == true) {
							// 进入当前目录,该目录下应该只有一个文件
							boolean changeDayFlag = ftpClient.changeWorkingDirectory(dayString);
							if (changeDayFlag == true) {
								for (FTPFile needFile : ftpClient.listFiles()) {
									System.out.println(needFile.getName());
									downloadOneFile(dayString, needFile.getName());
								}
								ftpClient.changeToParentDirectory();
							}
							ftpClient.changeToParentDirectory();
						}
					}
					ftpClient.changeToParentDirectory();
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private boolean downloadOneFile(String dayString, String filename) {
		if (this.ftpClient == null) {
			return false;
		}
		OutputStream os = null;
		System.out.println("begin download file " + filename + " ...");
		String directoryString = LOCAL_DIRECTORY + File.separator + dayString;
		File directoryFile = new File(directoryString);
		if (directoryFile.exists() == false) {
			directoryFile.mkdirs();
		}
		File localFile = new File(directoryString + File.separator + filename);
		try {
			os = new FileOutputStream(localFile);
			ftpClient.retrieveFile(filename, os);
			os.close();
			System.out.println("download file " + filename + " finished!");
			return true;
		} catch (Exception e) {
			System.out.println("download file " + filename + " failed!");
			e.printStackTrace();
			return false;
		}
	}
	
	public static void main(String[] args) {
		FTPDataGetter ftpDataGetter = new FTPDataGetter();
		for (int year = 2018; year<=2018; year ++) {
			for (int month = 1; month <= 12; month ++) {
				for (int day = 10; day <= 20; day ++) {
					String dayString = String.format("%d%02d%02d", year, month, day);
					ftpDataGetter.downloadOneDayFiles(dayString);
				}
			}
		}
//		ftpDataGetter.downloadOneDayFiles("20180502");
	}
}

posted @ 2018-06-05 17:08  zifeiy  阅读(2842)  评论(0编辑  收藏  举报