且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java base64编码的三种方式

更新时间:2021-07-03 05:55:13

Java 中如何使用base64编码呢?

有如下三种方式:

方式一:commons-codec.jar

Java代码  java base64编码的三种方式
  1. String base64String = "whuang123";  
  2.         byte[] result = Base64.encodeBase64(base64String.getBytes());  

 

方式二:使用sun.misc.BASE64Encoder

Java代码  java base64编码的三种方式
  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代码  java base64编码的三种方式
  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代码  java base64编码的三种方式
  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. }  

 运行结果如下:
java base64编码的三种方式