本文實(shí)例為大家分享了java使用base64編碼的具體代碼,供大家參考,具體內(nèi)容如下
test base64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.weiwen.provider.utils; import java.io.ioexception; import com.alibaba.fastjson.json; import lombok.extern.slf4j.slf4j; import org.junit.test; import sun.misc.base64encoder; import sun.misc.base64decoder; @slf4j public class base64 { @test public void testbase64() throws ioexception { // base64編碼 string s = "1f2bc1970a2eb19aabc0f94acea922717a1ae998603ff0593baff" ; base64encoder encoder = new base64encoder(); s = encoder.encode(s.getbytes( "utf-8" )); // system.out.println(s); log.info( "base64編碼為:{}" , json.tojsonstring(s)); // base64解碼 base64decoder decoder = new base64decoder(); byte [] bytes = decoder.decodebuffer(s); // system.out.println(new string(bytes, "utf-8")); log.info( "base64解碼為:{}" , json.tojsonstring( new string(bytes, "utf-8" ))); } } |
base64工具類(lèi)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package com.weiwen.provider.utils; import java.io.ioexception; import com.alibaba.fastjson.json; import lombok.extern.slf4j.slf4j; import org.junit.test; import sun.misc.base64encoder; import sun.misc.base64decoder; @slf4j public class base64 { /** * base64 編碼 * @param encodetext * @return * @throws ioexception */ public static string base64encode(string encodetext) throws ioexception{ base64encoder encoder = new base64encoder(); string str = encoder.encode(encodetext.getbytes( "utf-8" )); log.info( "base64編碼為:{}" , json.tojsonstring(str)); return str; } /** * base64 解碼 * @param decodetext * @return * @throws ioexception */ public static byte [] base64decode(string decodetext) throws ioexception{ base64decoder decoder = new base64decoder(); byte [] bytes = decoder.decodebuffer(decodetext); log.info( "base64解碼為:{}" , json.tojsonstring( new string(bytes, "utf-8" ))); return bytes; } } |
以上所述是小編給大家介紹的java使用base64編碼詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://blog.csdn.net/weixin_42740530/article/details/88249641