2010年1月17日 星期日

Java Base64 Encode & Decode(編碼和解碼)

1. 下載所需的Jar檔。(這裡我們是使用Apache的commons-codec套件)

2. import org.apache.commons.codec.binary.Base64;

3. 使用Base64 Class進行編碼與解碼
Base64 base64 = new Base64();
//使用Base64進行字串編碼
String encodeString = new String(base64.encode("This is source string.".getBytes()));
//輸出結果將為"VGhpcyBpcyBzb3VyY2Ugc3RyaW5nLg=="
System.out.println(encodeString);
//使用Base64進行字串解碼
String decodeString = new String(base64.decode(encodeString.getBytes()));
//輸出結果將為"This is source string."
System.out.println(decodeString);


沒有留言:

張貼留言