Archives: Apache Commons

Java – Connect to FTP Server

[code lang=”java”]import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;

public class FtpConnectDemo {

public static void main(String[] args) {
FTPClient client = new FTPClient();

client.connect("ftp.domain.com");
boolean login = client.login("admin", "secret");

if (login) {
System.out.println("Login success…");
boolean logout = client.logout();
if (logout) {
System.out.println("Logout from FTP server…");
}
} else {
System.out.println("Login fail…");
}
client.disconnect();
}

}[/code]


Java – Generate Random alpha-numeric String

The code below show you how to use the apache commons RandomStringUtils class to generate some random string data.

Output:

random = 2377316738997278218735578855577798247976319451877477254850564896
random = wwqbLxlRynIdptAoxuSIfbABRoOFHLyKaFEscrUoBQjAHPOkcIqfHuMnhXVzaLCf
random = 6nQ0PJf<#VgIi=’&d*Ru#i_exMoHXb
random = ZGbelXYEiLrlLBXKroCIDJPrevZPIGkD

Java – Convert Array to Map using Commons ArrayUtils

This example use the Apache Commons lang’s ArrayUtils class to convert a two dimensional array to a Map object.