Wednesday, July 16, 2008

Generate Random Alphanumeric Random String of N Characters with Groovy or Java

I was googling for a Alpha-numeric Random String generation. I come up with two solution. One option is to generate Random Alphanumeric Strings with RandomStringUtils available on Apache Common . The following section of code is borrowed from java2s.com


import org.apache.commons.lang.RandomStringUtils;
public class RandomStringUtilsTrial {
public static void main(String[] args) {

//Random 8 chars string where letters are enabled while numbers are not.
System.out.print("8 char string using letters but no numbers >>>");
System.out.println(RandomStringUtils.random(8, true, false));

}
}


Other Option can be using the Java Uitil class Random as shown below. You don’t have to worry about Apache Common API. The following code section (written for Groovy) generates 100 '32 characters' long Alphanumeric Random string.



private static String validChars ="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"
private int IDlength=32
int maxIndex = validChars.length()
for(i in 0..99)
{ String resultID = ""
java.util.Random rnd = new java.util.Random(System.currentTimeMillis()*(new java.util.Random().nextInt()))
for ( i in 0..IDlength ) {
int rndPos = Math.abs(rnd.nextInt() % maxIndex);
resultID += validChars.charAt(rndPos)
number=resultID
}

println resultID
}



Good luck !
Read More...

Thursday, July 10, 2008

cURL , A command line tool with URL Support !

One of my co-worker introduced a tool named cURL to me today. It a quite handy command line tool for transferring files with URL Syntax supporting most of the interner protocols like FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. curl supports SSL certificates, HTTP POST, HTTP PUT, proxy tunneling , HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos and many more)

You can download cURL from http://curl.haxx.se/ . It’s a free and open software tools.

For example curl http://nepalnews.com : return a html version of your web page in your console window. Its detail tutorial and example can be found at http://curl.haxx.se/docs/httpscripting.html

I have use curl on the following
curl -X POST -H 'Content-type: text/xml' -d @ http://myserver.com/process.gsp < firstfocus.xml

Here I am sending an xml file, firstfocus.xml, to the server myserver.com . process.gsp do calls the controller which helps in the business logic at the back end and return the result on my console window. It’s a pretty good tool for testing and development environment.
Thanks to Joshua for letting me know about this tool and its usage.
Read More...

Pages

 ©mytechtoday.com 2006-2010

 ©Mytechtoday

TOP