java高仿新浪微博短链接地址生成工具ShortUrlGenerator.java
仿新浪微博 短链接地址生成工具
ShortUrlGenerator.java
String sLongUrl = "http://tech.sina.com.cn/i/2011-03-23/11285321288.shtml"; // 3BD768E58042156E54626860E241E999
String[] aResult = shortUrl(sLongUrl);
// 打印出结果
for (int i = 0; i < aResult.length; i++) {
System.out.println("[" + i + "]:::" + aResult[i]);
}
///// 控制台输出如下: [0]:::7nUFJn [1]:::f6Zzy2 [2]:::j6jmQb [3]:::2eAjea
【附:MD5.java】
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
public static void main(String arg[]) {
String myinfo = "我的测试信息";
System.out.println(myinfo);
System.out.println(md5Encode(myinfo));
}
public static String md5Encode(String input) {
try {
MessageDigest alg = MessageDigest.getInstance("MD5");
alg.update(input.getBytes());
byte[] digesta = alg.digest();
return byte2Hex(digesta);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
//转换下
static String byte2Hex(byte[] paramArrayOfByte) {
StringBuffer localStringBuffer = new StringBuffer();
String str = "";
for (int i = 0; i < paramArrayOfByte.length; ++i) {
str = Integer.toHexString(paramArrayOfByte[i] & 0xFF);
if (str.length() == 1)
localStringBuffer.append("0");
localStringBuffer.append(str);
}
return localStringBuffer.toString().toUpperCase();
}
}
由最代码官方编辑于2014-9-9 9:31:28