org.apache.commons.net.ftp.FTPSClient 的使用

API地址 :http://commons.apache.org/net/apidocs/org/apache/commons/net/ftp/FTPSClient.html

 

本机测试环境: 自己搭建FTP 服务器,使用工具Serv-U。

 

主要类的使用:

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.util.TimeZone;

import javax.mail.Flags.Flag;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.util.StringUtils;

public class FTP {
	private final static Log log = LogFactory.getLog(FTP.class);
	
	private FTPClient fClient;
	
	private String username;	//登陆FTP 用户名
	private String password;	//用户密码,支持强密码
	private String url;			//FTP 地址
	private int port;
	private String remoteDir;
	
	public FTP(String username, String password, String url, int port,
			String remoteDir) {
		super();
		this.username = username;
		this.password = password;
		this.url = url;
		this.port = port;
		this.remoteDir = remoteDir;
		fClient = new FTPClient();
		this.ftpLogin();	
	}
	
	/**
	 * 登陆FTP服务器
	 * @throws Exception 
	 */
	public boolean ftpLogin(){
		boolean flag = false;
		try {
			//FTP参数设置
			FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_NT);		
			config.setServerTimeZoneId(TimeZone.getDefault().getID());			
			this.fClient.setControlEncoding("GBK");
			this.fClient.configure(config);	
			if(this.port > 0) {
				this.fClient.connect(url, port);
			} else {
				this.fClient.connect(url);//到生产环境中不能用端口号
			}
			int reply = this.fClient.getReplyCode();
			if(!FTPReply.isPositiveCompletion(reply)) {
		        this.fClient.disconnect();
		        log.info("FTP server refused connection." + url );
		        // TODO 
		        return false;
		     }
			this.fClient.login(this.username, this.password);
			this.fClient.changeWorkingDirectory(this.remoteDir);
			this.fClient.setFileType(FTPClient.BINARY_FILE_TYPE);	
			log.info("成功登陆FTP服务器:" + this.url +" 端口号:" + this.port +" 目录:" + remoteDir);
			flag = true;
		} catch (SocketException e) {			
			e.printStackTrace();
			 log.info("FTP server refused connection." + url);			 
		} catch (IOException e) {			
			e.printStackTrace();
		}
		return flag;
	}
	
	/**
	 * 关闭FTP 连接
	 */	
	public void close(){
		if(null != this.fClient && this.fClient.isConnected()){			
			try {
				this.fClient.logout();
				this.fClient.disconnect();
			} catch (IOException e) {					
				e.printStackTrace();
			}	
			log.info("close FTP server .");
		}
	}
	
	/**
	 * 上传文件
	 * @param localFilePath	本地文件路径及名称
	 * @param remoteFileName FTP 服务器文件名称
	 * @return
	 */
	public boolean uploadFile(String localFilePath, String remoteFileName){
		BufferedInputStream inStream = null;
		boolean success = false;
		try {
			inStream = new BufferedInputStream(new FileInputStream(localFilePath));
			success = this.fClient.storeFile(remoteFileName, inStream);
		} catch (FileNotFoundException e) {			
			e.printStackTrace();			
		} catch (IOException e){
			e.printStackTrace();
		}finally{
			if(inStream != null){
				try {
					inStream.close();
				} catch (IOException e) {				
					e.printStackTrace();
				}
			}
		}
		return success;
	}
	
	/**
	 * 下载文件
	 * @param localFilePath 本地文件名及路径
	 * @param remoteFileName 远程文件名称
	 * @return
	 */
	public boolean downloadFile(String localFilePath, String remoteFileName){
		BufferedOutputStream outStream = null;
		boolean success = false;
		try {
			outStream = new BufferedOutputStream(new FileOutputStream(
					localFilePath));
			success = this.fClient.retrieveFile(remoteFileName, outStream);
		} catch (FileNotFoundException e) {			
			e.printStackTrace();			
		} catch (IOException e){
			e.printStackTrace();
		}finally{
			if(outStream != null){
				try {
					outStream.close();
				} catch (IOException e) {					
					e.printStackTrace();
				}
			}
		}
		return success;
	}
	
	/**
	 * 获得FTP 服务器下所有的文件名列表
	 * @param regex
	 * @return
	 */
	public String[] getListFiels(String regex){
		String files [] = new String[0];
		try {
			files = this.fClient.listNames(regex);
		} catch (IOException e) {			
			e.printStackTrace();
		}
		return files;
	}
	
	/**
	 * 返回上一级目录(父目录)
	 */
	public void ToParentDir() {
		try {
			this.fClient.changeToParentDirectory();
		} catch (IOException e) {
			e.printStackTrace();
		}
		log.info("返回到上层目录。");
	}
	
	/**
	 * 变更工作目录
	 * 
	 * @param remoteDir--目录路径
	 */
	public void changeDir(String remoteDir) {
		try {
			this.remoteDir = remoteDir;
			this.fClient.changeWorkingDirectory(remoteDir);
		} catch (IOException e) {
			e.printStackTrace();
		}
		log.info("变更工作目录为:" + remoteDir);
	}
	
	/**
	 * 设置传输文件的类型[文本文件或者二进制文件]
	 * 
	 * @param fileType--BINARY_FILE_TYPE,ASCII_FILE_TYPE
	 */
	public void setFileType(int fileType) {
		try {
			fClient.setFileType(fileType);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public boolean deleteFile(String pathname){	
		boolean flag = false;		
		try{
			flag = this.fClient.deleteFile(pathname);
		}catch(Exception e){
			log.info("删除FTP文件失败 文件名:" + this.remoteDir + pathname );
		}
		if(flag) log.info("成功将文件删除 文件名:"+ this.remoteDir + pathname);
		return flag;
	}
	/**
	 * 检查FTP 是否关闭 ,如果关闭打开FTP
	 * @throws Exception 
	 */
	public boolean openFtpConnection(){	
		if(null == this.fClient)return false;
		boolean flag = true;
		try{
			if(!this.fClient.isConnected()){
				flag = this.ftpLogin();
			}
		}catch(Exception e){
			e.printStackTrace();
			flag = false;
		}		
		return flag;
	}
	
}
  1. 遇到过的问题:  在登陆FTP 服务器时出现过登陆阻塞,原来是不需要使用端口号进行连接。
  2. FTP 在登陆系统是,如果客户服务器没有相应,程序可能一直阻塞在哪里,需要处理登陆超时。
  3. 在下载、上传文件时需要注意取、发文件策略。
  4. 总结:   

你可能感兴趣的:(apache,.net,工作)