FTPClient下载文件及目录

package ftptest2;

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.log4j.Logger;

public class FtpTool {
	private static final Logger log = Logger.getLogger(FtpUtil.class);

	private static String SERVER_NAME = "192.168.18.43";
	private static final int SERVER_PORT = 21;
	private static String LOGIN_NAME = "test";
	private static String LOGIN_PASSWD = "test";
	private static String LOCAL_PATH = "E:\\data\\";
	private static String REMOTE_PATH = ".";// 代表根目录
	// private static String REMOTE_PATH = "ttttt";

	private static FTPClient ftpClient = new FTPClient();

	public static void main(String[] args) throws Exception {

		FtpTool downFTPClient = new FtpTool();
		downFTPClient.getConnection();
		downloadAll(downFTPClient, LOCAL_PATH, REMOTE_PATH, true);
		downFTPClient.disConnection();
	}

	// 删除全部文件
	private static void downloadAll(FtpTool downFTPClient, String localPath,
			String remotePath, boolean isDeleteFile) {
		try {
			FTPFile[] files = null;
			boolean changedir = ftpClient.changeWorkingDirectory(REMOTE_PATH);
			if (changedir) {
				files = ftpClient.listFiles();
				for (FTPFile file : files) {
					downFTPClient.downloadFile(file, LOCAL_PATH, REMOTE_PATH,
							true);
				}
			}
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	/**
	 * 断开连接
	 * 
	 * @throws Exception
	 */
	public void disConnection() throws Exception {
		if (ftpClient.isConnected()) {
			ftpClient.logout();
			ftpClient.disconnect();
		}
	}

	/**
	 * 建立连接
	 */
	private void getConnection() {
		try {
			// 解决中文乱码问题
			ftpClient.setControlEncoding("utf-8");
			ftpClient.connect(SERVER_NAME, SERVER_PORT);
			ftpClient.login(LOGIN_NAME, LOGIN_PASSWD);

		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * 下载文件
	 */
	private void downloadFile(FTPFile ftpFile, String relativeLocalPath,
			String relativeRemotePath, boolean isDeleteFile) {
		if (ftpFile.isFile()) {// down file
			if (ftpFile.getName().indexOf("?") == -1) {
				OutputStream outputStream = null;
				try {
					outputStream = new FileOutputStream(relativeLocalPath
							+ ftpFile.getName());
					log.info("download fileName: " + ftpFile.getName());
					ftpClient.retrieveFile(ftpFile.getName(), outputStream);
					outputStream.flush();
					outputStream.close();
				} catch (Exception e) {
					System.err.println(e);
				} finally {
					try {
						// 删除文件
						if (isDeleteFile) {
							log.info("delete fileName : " + ftpFile.getName());
							ftpClient.deleteFile(ftpFile.getName());
						}
						if (outputStream != null)
							outputStream.close();
					} catch (IOException e) {
						System.out.println("ShowasaFile");
					}
				}
			}
		} else { // deal dirctory
			String newlocalRelatePath = relativeLocalPath + ftpFile.getName();
			String newRemote = new String(relativeRemotePath
					+ ftpFile.getName().toString());
			File fl = new File(newlocalRelatePath);
			if (!fl.exists()) {
				fl.mkdirs();
			}
			try {
				newlocalRelatePath = newlocalRelatePath + '/';
				newRemote = newRemote + "/";
				String currentWorkDir = ftpFile.getName().toString();
				// enter relative workdirectory
				boolean changedir = ftpClient
						.changeWorkingDirectory(currentWorkDir);
				if (changedir) {
					FTPFile[] files = null;
					files = ftpClient.listFiles();
					for (int i = 0; i < files.length; i++) {
						downloadFile(files[i], newlocalRelatePath, newRemote,
								isDeleteFile);
						// removeDirectory()方法只能删除空目录,因此要先删除目录中的文件。
						log.info("delete directory : " + files[i].getName());
						ftpClient.removeDirectory(files[i].getName());
					}
					// 删除当前目录
					log.info("delete directory : ");
					ftpClient.removeDirectory(".");
				}
				// return parent directory
				if (changedir)
					ftpClient.changeToParentDirectory();
			} catch (Exception e) {
				System.out.println(e);
			}
		}
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值