1. 程式人生 > >java base64編碼的三種方式

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.      * @param str 
  14.      * @return string 
  15.      */  
  16.      public static byte[] decode(String str) {  
  17.      byte[] bt = null;  
  18.      try {  
  19.      sun.misc.BASE64Decoder decoder = new
     sun.misc.BASE64Decoder();  
  20.      bt = decoder.decodeBuffer(str);  
  21.      } catch (IOException e) {  
  22.      e.printStackTrace();  
  23.      }  
  24.      return bt;  
  25.      }  

方式三:使用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.      * decode by Base64 
  14.      */  
  15.     public static byte[] decodeBase64(String input) throws Exception {  
  16.         Class clazz = Class  
  17.                 .forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");  
  18.         Method mainMethod = clazz.getMethod("decode", String.class);  
  19.         mainMethod.setAccessible(true);  
  20.         Object retObj = mainMethod.invoke(null, input);  
  21.         return (byte[]) retObj;  
  22.     }  

測試:

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

 執行結果如下:

 

推薦使用方式一