本文实例为大家分享了java使用base64编码的具体代码,供大家参考,具体内容如下
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工具类
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编码详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/weixin_42740530/article/details/88249641