Java Code Examples for org.apache.commons.net.ftp.FTPClient#addProtocolCommandListener()

The following examples show how to use org.apache.commons.net.ftp.FTPClient#addProtocolCommandListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Ftp.java    From Lottery with GNU General Public License v2.0 6 votes vote down vote up
private FTPClient getClient() throws SocketException, IOException {
	FTPClient ftp = new FTPClient();
	ftp.addProtocolCommandListener(new PrintCommandListener(
			new PrintWriter(System.out)));
	ftp.setDefaultPort(getPort());
	ftp.connect(getIp());
	int reply = ftp.getReplyCode();
	if (!FTPReply.isPositiveCompletion(reply)) {
		log.warn("FTP server refused connection: {}", getIp());
		ftp.disconnect();
		return null;
	}
	if (!ftp.login(getUsername(), getPassword())) {
		log.warn("FTP server refused login: {}, user: {}", getIp(),
				getUsername());
		ftp.logout();
		ftp.disconnect();
		return null;
	}
	ftp.setControlEncoding(getEncoding());
	ftp.setFileType(FTP.BINARY_FILE_TYPE);
	ftp.enterLocalPassiveMode();
	return ftp;
}
 
Example 2
Source File: FTPClientTemplate.java    From MicroCommunity with Apache License 2.0 5 votes vote down vote up
/** 
 * 返回一个FTPClient实例 
 *  
 * @throws Exception
 */  
private FTPClient getFTPClient()throws Exception {
    if (ftpClientThreadLocal.get() != null && ftpClientThreadLocal.get().isConnected()) {  
        return ftpClientThreadLocal.get();  
    } else {  
        FTPClient ftpClient = new FTPClient(); //构造一个FtpClient实例
        ftpClient.setControlEncoding(encoding); //设置字符集  
  
        connect(ftpClient); //连接到ftp服务器  
  
        //设置为passive模式  
        if (passiveMode) {  
            ftpClient.enterLocalPassiveMode();  
            String replystr=ftpClient.getReplyString();
            ftpClient.sendCommand("PASV");
            replystr=ftpClient.getReplyString();
        }  
        setFileType(ftpClient); //设置文件传输类型  
  
        try {  
            ftpClient.setSoTimeout(clientTimeout);  
            ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        } catch (SocketException e) {
            throw new Exception("Set timeout error.", e);
        }  
        ftpClientThreadLocal.set(ftpClient);  
        return ftpClient;  
    }  
}
 
Example 3
Source File: FTPServerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private FTPClient connectClient() throws IOException
{
    FTPClient ftp = new FTPClient();

    if(logger.isDebugEnabled())
    {
        ftp.addProtocolCommandListener(new PrintCommandListener(
                                   new PrintWriter(System.out)));
    }
    ftp.connect(HOSTNAME, ftpConfigSection.getFTPPort());
    return ftp;
}
 
Example 4
Source File: FTPUploader.java    From journaldev with MIT License 5 votes vote down vote up
public FTPUploader(String host, String user, String pwd) throws Exception{
	ftp = new FTPClient();
	ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
	int reply;
	ftp.connect(host);
	reply = ftp.getReplyCode();
	if (!FTPReply.isPositiveCompletion(reply)) {
		ftp.disconnect();
		throw new Exception("Exception in connecting to FTP Server");
	}
	ftp.login(user, pwd);
	ftp.setFileType(FTP.BINARY_FILE_TYPE);
	ftp.enterLocalPassiveMode();
}
 
Example 5
Source File: FTPDownloader.java    From journaldev with MIT License 5 votes vote down vote up
public FTPDownloader(String host, String user, String pwd) throws Exception {
    ftp = new FTPClient();
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    int reply;
    ftp.connect(host);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new Exception("Exception in connecting to FTP Server");
    }
    ftp.login(user, pwd);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.enterLocalPassiveMode();
}
 
Example 6
Source File: FtpClient.java    From tutorials with MIT License 5 votes vote down vote up
void open() throws IOException {
    ftp = new FTPClient();

    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

    ftp.connect(server, port);
    int reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new IOException("Exception in connecting to FTP Server");
    }

    ftp.login(user, password);
}