FTPUtil

本文主要介绍了FTPUtil的使用,包括需要的jar包如log4j.jar、commoms-net-1.4.1.jar和jakarta-oro-2.0.5.jar的详细信息。
摘要由CSDN通过智能技术生成

jar包:log4j.jar  commoms-net-1.4.1.jar  jakarta-oro-2.0.5.jar

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;

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

public class FtpUtil {
	private static Logger log=Logger.getLogger(FtpUtil.class.getName());
	private static String charSet="UTF-8";
	
	/**
	 * 连接ftp
	 */
	public static FTPClient getFtpClient(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort) {
		FTPClient ftpClient = new FTPClient();
		try {
			ftpClient.connect(ftpHost, ftpPort);
			ftpClient.login(ftpUserName, ftpPassword);
			ftpClient.setControlEncoding(charSet);
			
			if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
				log.debug("未连接到FTP,用户名或密码错误!");
				ftpClient.disconnect();
			}else {
				log.info("FTP连接成功!");
			}
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			log.error("IP地址可能错误,请正确配置!");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("FTP端口错误,请正确配置!");
			e.printStackTrace();
		}
		return ftpClient;
	}
	
	/**
	 * 下载文件
	 */
	public static boolean downloadFile(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort, String ftpPath, String localPath, String fileName) {
		boolean downloadResult = false;
		FTPClient ftpClient = null;
		try {
			ftpClient=getFtpClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
			ftpClient.setControlEncoding(charSet);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.enterLocalPassiveMode();
			ftpClient.changeWorkingDirectory(ftpPath);
			FTPFile[] files=ftpClient.listFiles(ftpPath);
			
			boolean fileExist=false;
			for(FTPFile file:files) {
				if(file.getName().equals(fileName)) {
					fileExist=true;
				}else {
					fileExist=false;
				}
			}
			if(fileExist) {
				File loadFile = new File(localPath + File.separator + fileName);
				OutputStream out = new FileOutputStream(loadFile);
				ftpClient.retrieveFile(fileName, out);
				out.close();
				
				//删除ftp文件
				ftpClient.deleteFile(ftpPath + File.separator + fileName);
				ftpClient.logout();
				downloadResult=true;
			}else {
				log.error("没有找到"+fileName+"文件!");
			}
		}catch(FileNotFoundException e) {
			log.error("没有找到"+fileName+"文件!");
			downloadResult=false;
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("文件读取错误!");
			downloadResult=false;
			e.printStackTrace();
		}
		return downloadResult;
	}
	
	/**
	 * 上传文件
	 */
	
	public static boolean uploadFile(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort, String ftpPath, String fileName, String localPath) {
		boolean uploadResult = false;
		FTPClient ftpClient = null;
		try {
			ftpClient=getFtpClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
			int reply=ftpClient.getReplyCode();
			if(!FTPReply.isPositiveCompletion(reply)) {
				ftpClient.disconnect();
				return uploadResult;
			}
			ftpClient.setControlEncoding(charSet);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.enterLocalPassiveMode();
			ftpClient.changeWorkingDirectory(ftpPath);
			
			FileInputStream in=new FileInputStream(new File(localPath));
			ftpClient.storeFile(fileName, in);
			in.close();
			ftpClient.logout();
			uploadResult=true;
		}catch(IOException e) {
			log.error("文件读取错误!");
			e.printStackTrace();
		}finally {
			if(ftpClient!=null) {
				try {
					ftpClient.disconnect();
				}catch(IOException e) {
					e.printStackTrace();
				}
			}
		}
		return uploadResult;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值