Base64 編解碼

測試Apache Base64和sun公司專有包BASE64Decoder、BASE64Encoder的編解碼是否一致的代碼:java

import java.util.Arrays;

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

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * Base64 測試
 *
 * @author Kevin
 * @date 2016-04-27
 */
public class Base64Test {

    public static void main(String[] args) throws Exception {
        String base64DataStr = "+/whuang123FDKASLJFLDSA+/IJFIDSAFKLD";
        String apacheBase64Data = Base64.encodeBase64String(base64DataStr.getBytes("UTF-8"));
        String sunBase64Data = new BASE64Encoder().encode(base64DataStr.getBytes("UTF-8"));

        // 測試Apache Base64的編碼和sun公司專有包BASE64Encoder的編碼是否一致
        boolean isEqualsStr = apacheBase64Data.equals(sunBase64Data);
        // 結果爲true
        System.out.println(isEqualsStr);

        byte[] apacheBase64Result = Base64.decodeBase64(apacheBase64Data);

		BASE64Decoder decoder = new BASE64Decoder();
        byte[] sunBase64DecoderResult = decoder.decodeBuffer(sunBase64Data);

        // 測試Apache Base64的解碼和sun公司專有包BASE64Decoder的解碼是否一致
        boolean isEquals = Arrays.equals(apacheBase64Result,sunBase64DecoderResult);
        // 結果爲true
        System.out.println(isEquals);
    }

}

測試結論apache

Base64的編解碼優先使用Apache Base64。測試

相關文章
相關標籤/搜索