Skip to content

Instantly share code, notes, and snippets.

@kranthilakum
Created December 23, 2012 13:42
Show Gist options
  • Save kranthilakum/4363479 to your computer and use it in GitHub Desktop.
Save kranthilakum/4363479 to your computer and use it in GitHub Desktop.
A class to illustrate the use of Apache Commens Net FTP connection. The FTPClient verifies and establishes a connection to an FTP server.
/**
* A class to illustrate the use of Apache Commens Net FTP connection.
*
* @author Kranthi Lakum
*/
public class FileTransfer
{
private static FTPClient theFtpClient = new FTPClient();
private static int theCode;
private static Logger myLogger = LoggerFactory.getLogger(FileTransfer.class);
public static void connect(String parUserName, String parPassword,
String parHostName, int parPort)
{
try
{
theFtpClient.connect(parHostName, parPort);
theFtpClient.login(parUserName, parPassword);
theCode = theFtpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(theCode))
{
System.out.println("Connection Success!");
}
else
{
System.out.println("Connection Failed.");
theFtpClient.disconnect();
}
}
catch (SocketException e1)
{
myLogger.error("Socket unresponsive on server.");
}
catch (IOException e2)
{
myLogger.error("An I/O error while talking to server.");
}
finally
{
if(theFtpClient.isConnected())
{
try
{
theFtpClient.disconnect();
}
catch(IOException ioe)
{
// do nothing
}
}
}
public static void main (String[] args)
{
String theUserName = "username"; // username of an FTP account on server
String thePassword = "password"; // relevant password
String theRemoteHost = "127.0.0.1"; // host name or IP address of the FTP server
int thePortNumber = 21; // port number to listen to
connect(theUserName, thePassword, theRemoteHost, thePortNumber);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment