Java如何進行Base64的編碼(Encode)與解碼(Decode)?

關(guān)于base64編碼Encode和Decode編碼的幾種方式

Base64是一種能將任意Binary資料用64種字元組合成字串的方法,而這個Binary資料和字串資料彼此之間是可以互相轉(zhuǎn)換的,十分方便。在實際應(yīng)用上,Base64除了能將Binary資料可視化之外,也常用來表示字串加密過后的內(nèi)容。如果要使用Java 程式語言來實作Base64的編碼與解碼功能,可以參考本篇文章的作法。

早期作法

早期在Java上做Base64的編碼與解碼,會使用到JDK里sun.misc套件下的BASE64Encoder和BASE64Decoder這兩個類別,用法如下:

<span style="font-size: medium;">final BASE64Encoder encoder = newBASE64Encoder();

final BASE64Decoder decoder = newBASE64Decoder();

final String text = "字串文字";

final byte[] textByte = text.getBytes("UTF-8");

//編碼

final String encodedText = encoder.encode(textByte);

System.out.println(encodedText);

//解碼

System.out.println(newString(decoder.decodeBuffer(encodedText), "UTF-8"));


final BASE64Encoder encoder = newBASE64Encoder();

final BASE64Decoder decoder = newBASE64Decoder();

final String text = "字串文字";

final byte[] textByte = text.getBytes("UTF-8");

//編碼

final String encodedText = encoder.encode(textByte);

System.out.println(encodedText);


//解碼

System.out.println(newString(decoder.decodeBuffer(encodedText), "UTF-8"));</span>

從以上程式可以發(fā)現(xiàn),在Java用Base64一點都不難,不用幾行程式碼就解決了!只是這個sun.mis c套件所提供的Base64功能,編碼和解碼的效率并不太好,而且在以后的Java版本可能就不被支援了,完全不建議使用。

Apache Commons Codec作法

Apache Commons Codec有提供Base64的編碼與解碼功能,會使用到org.apache.commons.codec.binary套件下的Base64類別,用法如下:

<span style="font-size: medium;">final Base64 base64 = newBase64();

final String text = "字串文字";

final byte[] textByte = text.getBytes("UTF-8");

//編碼

final String encodedText = base64.encodeToString(textByte);

System.out.println(encodedText);

//解碼

System.out.println(newString(base64.decode(encodedText), "UTF-8"));


final Base64 base64 = newBase64();

final String text = "字串文字";

final byte[] textByte = text.getBytes("UTF-8");

//編碼

final String encodedText = base64.encodeToString(textByte);

System.out.println(encodedText);

//解碼

System.out.println(newString(base64.decode(encodedText), "UTF-8"));</span>

以上的程式碼看起來又比早期用sun.mis c套件還要更精簡,效能實際執(zhí)行起來也快了不少。缺點是需要引用Apache Commons Codec,很麻煩。

Java 8之后的作法

Java 8的java.util套件中,新增了Base64的類別,可以用來處理Base64的編碼與解碼,用法如下:

<span style="font-size: medium;">final Base64.Decoder decoder = Base64.getDecoder();

final Base64.Encoder encoder = Base64.getEncoder();

final String text = "字串文字";

final byte[] textByte = text.getBytes("UTF-8");

//編碼

final String encodedText = encoder.encodeToString(textByte);

System.out.println(encodedText);

//解碼

System.out.println(newString(decoder.decode(encodedText), "UTF-8"));


final Base64.Decoder decoder = Base64.getDecoder();

final Base64.Encoder encoder = Base64.getEncoder();

final String text = "字串文字";

final byte[] textByte = text.getBytes("UTF-8");

//編碼

final String encodedText = encoder.encodeToString(textByte);

System.out.println(encodedText);

//解碼

System.out.println(newString(decoder.decode(encodedText), "UTF-8"));</span>

與sun.mis c套件和Apache Commons Codec所提供的Base64編解碼器來比較的話,Java 8提供的Base64擁有更好的效能。實際測試編碼與解碼速度的話,Java 8提供的Base64,要比sun.mis c套件提供的還要快至少11倍,比Apache Commons Codec提供的還要快至少3倍。因此在Java上若要使用Base64,這個Java 8底下的java .util套件所提供的Base64類別絕對是首選!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 眾所周知,我們Java8推出后帶來了一系列的新特性,在Base64的編碼與解碼處理上也新增了處理,在Java8的j...
    雨落地箏閱讀 1,630評論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,591評論 19 139
  • 關(guān)于base64編碼Encode和Decode編碼的幾種方式 Base64是一種能將任意Binary資料用64種字...
    橫豎撇捺啊閱讀 1,032評論 0 1
  • 廢話不多說,自己進入今天的主題 1、面向?qū)ο蟮奶卣饔心男┓矫妫?答:面向?qū)ο蟮奶卣髦饕幸韵聨讉€方面: - 抽象:...
    傳奇內(nèi)服號閱讀 2,536評論 1 31
  • 今天是新生入學(xué)的第二天!早晨去送孩子上學(xué),其實今天孩子有點不舒服!但還是要送她去上學(xué),路上跟她說:孩子你是最堅強的...
    阿濤演藝閱讀 340評論 0 0

友情鏈接更多精彩內(nèi)容