国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

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

2018-12-06 15:56 作者:校招VIP  | 我要投稿

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

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

早期作法

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

<span style="font-size: medium;">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"));</span>

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

Apache Commons Codec作法

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

<span style="font-size: medium;">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"));</span>

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

Java 8之后的作法

Java 8的java.util套件中,新增了Base64的類(lèi)別,可以用來(lái)處理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(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"));</span>

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




Java如何進(jìn)行Base64的編碼(Encode)與解碼(Decode)?的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
巴南区| 正宁县| 固安县| 巩留县| 长治市| 靖江市| 东港市| 老河口市| 梓潼县| 马尔康县| 防城港市| 连城县| 五河县| 沈阳市| 乌恰县| 涪陵区| 大化| 资兴市| 额尔古纳市| 昌乐县| 和平区| 蒙山县| 鄂尔多斯市| 敦煌市| 修水县| 凤山市| 将乐县| 镇巴县| 当涂县| 佛坪县| 清水县| 宣城市| 卢氏县| 尼勒克县| 甘孜| 郯城县| 稻城县| 石景山区| 麦盖提县| 泾阳县| 西藏|