Pages Navigation Menu

Coding is much easier than you think

Getting Positive Completion reply from FTP Server using Java

Getting Positive Completion reply from FTP Server using Java

 

package com.simplecode.com;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

public class FtpLogin {
	public static void main(String[] args) throws IOException {
		FTPClient ftpClient = new FTPClient();
		String errorMessage = null;
		boolean result;
		try {
			// Connect to the localhost
			ftpClient.connect("localhost");
			int reply = ftpClient.getReplyCode();

	        if (!FTPReply.isPositiveCompletion(reply)) {
	            errorMessage = "FTP server refused connection: " + ftpClient.getReplyString();
	        } 
	        else 
	        {
			// login to ftp server
			result = ftpClient.login("admin", "admin");
			if (result == true) {
				System.out.println("Successfully logged in!");
			} else {
				System.out.println("Login Fail!");
				return;
			}
		} 
		}catch (FTPConnectionClosedException e) {
			e.printStackTrace();
		} finally {
			try {
				ftpClient.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.out.println(e);
			}
		}

	}
}

 

The function ftpClient.getReplyCode(); is used to get reply code on sending ftp connection, and the command FTPReply.isPositiveCompletion(reply) is used to check weather the reply code is positive or not.

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.