1. 程式人生 > >Base64 Java org.apache.commons.codec.binary.Base64

Base64 Java org.apache.commons.codec.binary.Base64

Base64組成由 26個區分大小寫字母  + 10個數字 + 一個加號 和 一個/

package com.base64;

import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;

public class Base64_ {
	public static String base64encode(String message) {
		try {
			byte[] encodeBase64 = Base64.encodeBase64(message.getBytes("UTF-8"));
			System.out.println("Result:" + new String(encodeBase64));
			return new String(encodeBase64);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException();
		}

	}
	
	public static String base64decode(String message) {
		byte[] encodeBase64 = Base64.decodeBase64(message);
		System.out.println("Result:" + new String(encodeBase64));
		return new String(encodeBase64);

	}

}
<pre name="code" class="java">package com.base64;



public class Base64Test {

	public static void main(String[] args) {

		String str = "Hello World";	
		String str2 = Base64_.base64encode(str);
		System.out.println(str2);
		String str1 = Base64_.base64decode(str2);
		System.out.println(str1);
	}


}


需要引包   import org.apache.commons.codec.binary.Base64;