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
    评论
Java中的Base64编码可以使用多种方式进行。在Java 8中,可以使用java.util包下的Base64类来处理Base64编码和解码。可以通过以下步骤进行编码和解码: 1. 首先,创建一个Base64编码器和解码器对象: ``` final Base64.Encoder encoder = Base64.getEncoder(); final Base64.Decoder decoder = Base64.getDecoder(); ``` 2. 接下来,准备需要编码的文本,将其转换为字节数组: ``` final String text = "字串文字"; final byte[] textByte = text.getBytes("UTF-8"); ``` 3. 进行编码,将字节数组转换为Base64字符串: ``` final String encodedText = encoder.encodeToString(textByte); System.out.println(encodedText); ``` 4. 进行解码,将Base64字符串转换为字节数组,并重新转换为文本: ``` System.out.println(new String(decoder.decode(encodedText), "UTF-8")); ``` 关于Base64编码和解码,还有其他方式可以实现。在早期的Java版本中,可以使用sun.misc套件下的BASE64Encoder和BASE64Decoder类。这种方式的缺点是效率较低。示例代码如下: ``` 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")); ``` 另外,还可以使用org.apache.commons.codec.binary.Base64包下的Base64类进行编码和解码。这种方式相比sun包更加精简,并且执行效率更高。示例代码如下: ``` 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")); ``` 总而言之,Java中有多种方式可以进行Base64编码和解码,包括java.util包下的Base64类、sun.misc包下的BASE64Encoder和BASE64Decoder类以及org.apache.commons.codec.binary.Base64包下的Base64类。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值