通过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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

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

public class FtpUtil {
	public static boolean uploadFile(String url,// FTP服务器hostname
			int port,// FTP服务器端口
			String username, // FTP登录账号
			String password, // FTP登录密码
			String path, // FTP服务器保存目录
			String filename, // 上传到FTP服务器上的文件名
			InputStream input // 输入流
	) {
		boolean success = false;
		FTPClient ftp = new FTPClient();
		try {
			int reply;
			ftp.connect(url, port);// 连接FTP服务器
			// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
			ftp.login(username, password);// 登录
			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return success;
			}
			ftp.enterLocalPassiveMode();
			ftp.changeWorkingDirectory(path);
			ftp.setFileType(FTP.ASCII_FILE_TYPE);
			String FtpFilename = new String(filename.getBytes("GBK"),
					"iso-8859-1");
			boolean result = ftp.storeFile(FtpFilename, input);
			input.close();
			ftp.logout();
			success = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException ioe) {
				}
			}
		}
		return success;
	}

	/**
	 * Description: 从FTP服务器下载文件
	 * 
	 * @Version1.0
	 * @param url
	 *            FTP服务器hostname
	 * @param port
	 *            FTP服务器端口
	 * @param username
	 *            FTP登录账号
	 * @param password
	 *            FTP登录密码
	 * @param remotePath
	 *            FTP服务器上的相对路径
	 * @param fileName
	 *            要下载的文件名
	 * @param localPath
	 *            下载后保存到本地的路径
	 * @return
	 */

	public static boolean downFile(String url, // FTP服务器
			int port,// FTP服务器端口
			String username, // FTP登录账号
			String password, // FTP登录密码
			String remotePath,// FTP服务器上的相对路径
			String fileName,// 要下载的文件名
			String localPath// 下载后保存到本地的路径

	) {

		boolean success = false;
		FTPClient ftp = new FTPClient();
		try {
			int reply;
			ftp.connect(url, port);
			ftp.enterLocalPassiveMode();
			// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
			ftp.login(username, password);// 登录
			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return success;
			}
			ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
			FTPFile[] fs = null;
			String[] names = null;
			if (ftp.listFiles() != null) {
				fs = ftp.listFiles();
			}
			if (ftp.listNames().length > 0) {
				names = ftp.listNames();
			}
			if (fs != null && fs.length > 0) {
				for (FTPFile ff : fs) {
					String FtpFilename = new String(ff.getName().getBytes(
							"iso-8859-1"), "GBK");
					if (FtpFilename.equals(fileName)) {
						File localFile = new File(localPath + "/" + FtpFilename);
						if (!localFile.exists()) {
							OutputStream is = new FileOutputStream(localFile);
							boolean result = ftp.retrieveFile(ff.getName(), is);
							is.close();
						}
					}
				}
			} else {
				if (names != null) {
					for (int i = 0, j = names.length; i < j; i++) {
						if (names[i].equals(fileName)) {
							File localFile = new File(localPath + "/"
									+ names[i]);
							if (!localFile.exists()) {
								OutputStream is = new FileOutputStream(
										localFile);
								boolean result = ftp.retrieveFile(names[i], is);
								is.close();
							}
						}
					}
				} else {
					ftp.logout();
					success = true;
					return success;
				}
			}
			ftp.logout();
			success = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException ioe) {
				}
			}
		}
		return success;
	}

	/**
	 * Description: 从FTP服务器删除文件
	 * 
	 * @Version1.0
	 * @param url
	 *            FTP服务器hostname
	 * @param port
	 *            FTP服务器端口
	 * @param username
	 *            FTP登录账号
	 * @param password
	 *            FTP登录密码
	 * @param remotePath
	 *            FTP服务器上的相对路径
	 * @param fileName
	 *            要下载的文件名
	 * @return
	 */

	public static boolean deleteFile(String url, // FTP服务器
			int port,// FTP服务器端口
			String username, // FTP登录账号
			String password, // FTP登录密码
			String remotePath,// FTP服务器上的相对路径
			String fileName// 要下载的文件名

	) {

		boolean success = false;
		FTPClient ftp = new FTPClient();
		try {
			int reply;
			ftp.connect(url, port);
			// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
			ftp.enterLocalPassiveMode();
			ftp.login(username, password);// 登录
			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return success;
			}
			ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
			FTPFile[] fs = ftp.listFiles();
			String[] names = ftp.listNames();

			if (fs != null && fs.length > 0) {
				for (FTPFile ff : fs) {
					String FtpFilename = new String(ff.getName().getBytes(
							"iso-8859-1"), "GBK");
					if (FtpFilename.equals(fileName)) {
						ftp.deleteFile(FtpFilename);
					}
				}
			} else {
				if (names != null) {
					// System.getProperties().getProperty("os.name").toUpperCase().startsWith("WIN");
					for (int i = 0, j = names.length; i < j; i++) {
						if (names[i].equals(fileName)) {
							ftp.deleteFile(names[i]);
						}
					}
				} else {
					ftp.logout();
					success = true;
					return success;
				}
			}
			ftp.logout();
			success = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException ioe) {
				}
			}
		}
		return success;
	}

	/**
	 * Description: 从FTP服务器下载文件,上传到ftp服务器
	 * 
	 * @Version1.0
	 * @param url
	 *            FTP服务器hostname
	 * @param port
	 *            FTP服务器端口
	 * @param username
	 *            FTP登录账号
	 * @param password
	 *            FTP登录密码
	 * @param remotePath
	 *            FTP服务器上的相对路径
	 * @param fileName
	 *            要下载的文件名
	 * @param localPath
	 *            下载后保存到本地的路径
	 * @return
	 */

	public static boolean downuploadFile(String url, // 下载FTP服务器
			int port,// 下载FTP服务器端口
			String username, // 下载FTP登录账号
			String password, // 下载FTP登录密码
			String remotePath,// 下载FTP服务器上的相对路径
			String fileName,// 要下载的文件名
			String urlup,// 上传FTP服务器hostname
			int portup,// 上传FTP服务器端口
			String usernameup, // 上传FTP登录账号
			String passwordup, // 上传FTP登录密码
			String pathup, // 上传FTP服务器保存目录
			String filenameup // 上传到FTP服务器上的文件名

	) {

		boolean success = false;
		FTPClient ftp = new FTPClient();
		try {
			int reply;
			ftp.connect(url, port);
			ftp.enterLocalPassiveMode();
			// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
			ftp.login(username, password);// 登录
			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return success;
			}
			ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
			FTPFile[] fs = null;
			String[] names = null;
			if (ftp.listFiles() != null) {
				fs = ftp.listFiles();
			}
			if (ftp.listNames().length > 0) {
				names = ftp.listNames();
			}
			if (fs != null && fs.length > 0) {
				for (FTPFile ff : fs) {
					String FtpFilename = new String(ff.getName().getBytes(
							"iso-8859-1"), "GBK");
					if (FtpFilename.equals(fileName)) {
						InputStream input=ftp.retrieveFileStream(fileName);
						
						int size=input.available();
				        if(size>0){
				        	Boolean result = FtpUtil.uploadFile(urlup,
									portup, usernameup,
									passwordup, pathup, filenameup,
										input);
				        }
						
						
					}
				}
			} else {
				if (names != null) {
					for (int i = 0, j = names.length; i < j; i++) {
						if (names[i].equals(fileName)) {
							InputStream input=ftp.retrieveFileStream(fileName);
							int size=input.available();
					        if(size>0){
					        	Boolean result = FtpUtil.uploadFile(urlup,
										portup, usernameup,
										passwordup, pathup, filenameup,
											input);
					        }
						}
					}
				} else {
					ftp.logout();
					success = true;
					return success;
				}
			}
			ftp.logout();
			success = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException ioe) {
				}
			}
		}
		return success;
	}
	
	
	public static void main(String[] args) throws Exception {
//      从ftp下载文件
//		Boolean result = FtpUtil.downFile("192.xxx.xxx.01",
//				21,"xx@outlook.com",
//				"qaz041366","/ftp", "log.txt",
//				"E:/iso/");
//		File file = new File("E:\\iso\\xx.txt");
//		if (file.exists()) {
//			return;
//		}
        //本地上传到FTP服务器
//		 Boolean result = FtpUtil.uploadFile("192.xxx.xxx.01",
//		 21, "xx@outlook.com",
//			"qaz0413xx", "/ftp", "xx.log",
//			(InputStream) (new FileInputStream(new File("E:\\iso\\xx.log"))));
//		if (!result) {
//			// file.delete();
//		}
		//从一个ftp下载并上传到另一个
		Boolean result = FtpUtil.downuploadFile("192.xxx.xxx.01",
				 21, "xxx@outlook.com",
					"qaz041366", "/ftp", "gioi_log.log","192.xxx.xxx.02",
					 21, "xxx@outlook.com",
						"qaz041366", "/ftp", "xx.log");
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值