java base64编码的三种方式

java 中如何使用base64编码呢?

有如下三种方式:

方式一:commons-codec.jar

Java代码   收藏代码
  1. String base64String = "whuang123";  
  2.         byte[] result = Base64.encodeBase64(base64String.getBytes());  

 

方式二:使用sun.misc.BASE64Encoder

Java代码   收藏代码
  1. /** 
  2.      * 编码 
  3.      *  
  4.      * @param bstr 
  5.      * @return String 
  6.      */  
  7.      public static String encode(byte[] bstr) {  
  8.      return new sun.misc.BASE64Encoder().encode(bstr);  
  9.      }  
  10.   
  11.     /** 
  12.      * 解码 
  13.      *  
  14.      * @param str 
  15.      * @return string 
  16.      */  
  17.      public static byte[] decode(String str) {  
  18.      byte[] bt = null;  
  19.      try {  
  20.      sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();  
  21.      bt = decoder.decodeBuffer(str);  
  22.      } catch (IOException e) {  
  23.      e.printStackTrace();  
  24.      }  
  25.       
  26.      return bt;  
  27.      }  

 

 

方式三:使用com.sun.org.apache.xerces.internal.impl.dv.util.Base64

Java代码   收藏代码
  1. /*** 
  2.      * encode by Base64 
  3.      */  
  4.     public static String encodeBase64(byte[] input) throws Exception {  
  5.         Class clazz = Class  
  6.                 .forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");  
  7.         Method mainMethod = clazz.getMethod("encode"byte[].class);  
  8.         mainMethod.setAccessible(true);  
  9.         Object retObj = mainMethod.invoke(nullnew Object[] { input });  
  10.         return (String) retObj;  
  11.     }  
  12.   
  13.     /*** 
  14.      * decode by Base64 
  15.      */  
  16.     public static byte[] decodeBase64(String input) throws Exception {  
  17.         Class clazz = Class  
  18.                 .forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");  
  19.         Method mainMethod = clazz.getMethod("decode", String.class);  
  20.         mainMethod.setAccessible(true);  
  21.         Object retObj = mainMethod.invoke(null, input);  
  22.         return (byte[]) retObj;  
  23.     }  

 

测试:

Java代码  
  1. package com.jn.base64;  
  2.   
  3. import junit.framework.Assert;  
  4.   
  5. import org.apache.commons.codec.binary.Base64;  
  6.   
  7. import com.common.util.SystemUtil;  
  8.   
  9. public class BaseTest {  
  10.     public static void main(String[] args) throws Exception {  
  11.         String base64String = "whuang123";  
  12.         byte[] result = Base64.encodeBase64(base64String.getBytes());  
  13.         SystemUtil.printBytes(result);  
  14.         byte[] result2 = SystemUtil.encode(base64String.getBytes()).getBytes();  
  15.         System.out.println("result2:"+result2);  
  16.         byte[] result3 = SystemUtil.encodeBase64(base64String.getBytes()).getBytes();  
  17.         boolean isSuccess = SystemUtil.isSame(result, result2);  
  18.         Assert.assertEquals(true, isSuccess);  
  19.         SystemUtil.printBytes(result2);  
  20.         SystemUtil.printBytes(result3);  
  21.         System.out.println(isSuccess);  
  22.     }  
  23. }  

 运行结果如下:

 

推荐使用方式一

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值