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

關于base64編碼Encode和Decode編碼的幾種方式

創(chuàng)新互聯(lián)主要為客戶提供服務項目涵蓋了網頁視覺設計、VI標志設計、營銷網站、網站程序開發(fā)、HTML5響應式網站建設成都做手機網站、微商城、網站托管及網站維護、WEB系統(tǒng)開發(fā)、域名注冊、國內外服務器租用、視頻、平面設計、SEO優(yōu)化排名。設計、前端、后端三個建站步驟的完善服務體系。一人跟蹤測試的建站服務標準。已經為成都人造霧行業(yè)客戶提供了網站營銷推廣服務。

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

早期作法

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

final BASE64Encoder encoder = new BASE64Encoder();
final BASE64Decoder decoder = new BASE64Decoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//編碼
final String encodedText = encoder.encode(textByte);
System.out.println(encodedText);
//解碼
System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));

final BASE64Encoder encoder = new BASE64Encoder();
final BASE64Decoder decoder = new BASE64Decoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//編碼
final String encodedText = encoder.encode(textByte);
System.out.println(encodedText);

//解碼
System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));

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

Apache Commons Codec作法

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

final Base64 base64 = new Base64();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//編碼
final String encodedText = base64.encodeToString(textByte);
System.out.println(encodedText);
//解碼
System.out.println(new String(base64.decode(encodedText), "UTF-8"));

final Base64 base64 = new Base64();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//編碼
final String encodedText = base64.encodeToString(textByte);
System.out.println(encodedText);
//解碼
System.out.println(new String(base64.decode(encodedText), "UTF-8"));

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

Java 8之后的作法

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

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(new String(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(new String(decoder.decode(encodedText), "UTF-8"));

與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類別絕對是首選!

到此這篇關于詳解Java如何進行Base64的編碼(Encode)與解碼(Decode)的文章就介紹到這了,更多相關Java Base64編碼與解碼內容請搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持創(chuàng)新互聯(lián)!

新聞名稱:詳解Java如何進行Base64的編碼(Encode)與解碼(Decode)
網頁地址:http://bm7419.com/article8/jjseop.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號品牌網站制作、網站收錄、網站營銷、微信小程序Google

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網站建設