BASE64编码和解码

 1 package com.utils.test;
 2 
 3 import org.apache.commons.codec.binary.Base64;
 4 import org.junit.Test;
 5 
 6 public class Base64Test {
 7 
 8     /**
 9      * 使用sun.misc.BASE64Encoder 和 sun.misc.BASE64Decoder
10      * 进行编码解码
11      * @throws Exception
12      */
13     @Test
14     public void base64Test1() throws Exception{
15         String str = "hello";
16         byte[] bytes = str.getBytes("utf-8");
17         // Base64编码
18         str = new sun.misc.BASE64Encoder().encode(bytes);
19         System.out.println("Base64编码: " + str);
20         // Base64解码
21         bytes = new sun.misc.BASE64Decoder().decodeBuffer(str);
22         str = new String(bytes, "utf-8");
23         System.out.println("Base64解码: " + str);
24     }
25     /**
26      * 需要 commons-codec-1.10.jar 
27      * 使用org.apache.commons.codec.binary.Base64
28      * 进行编码解码
29      * @throws Exception
30      */
31     @Test
32     public void base64Test2() throws Exception{
33         String str = "hello";
34         byte[] bytes = str.getBytes("utf-8");
35         //编码
36         bytes = Base64.encodeBase64(bytes);
37         str = new String(bytes,"utf-8");
38         System.out.println(str);
39         // 解码
40         bytes = Base64.decodeBase64(str);
41         str = new String(bytes, "utf-8");
42         System.out.println(str);
43     }
44     
45 }

 

转载于:https://www.cnblogs.com/jonny-xu/p/6393425.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值