java实现base64编码

Java代码  

  1. import java.util.ArrayList;  

  2. import java.util.List;  

  3.   

  4. import org.apache.commons.codec.binary.Base64;  

  5.   

  6. public class MyBase64 {  

  7.   

  8.     public static void main(String[] args) {  

  9.         // TODO Auto-generated method stub  

  10. //      char c  = '0';  

  11. //      int i  = (int)c;  

  12. //      System.out.println(i);  

  13. //      System.out.println(Integer.toBinaryString(i));  

  14.           

  15.         String s = "stringad";  

  16.         System.out.println(encode(s));  

  17.         System.out.println(encode(s.getBytes()));  

  18.         System.out.println(encode2(s.getBytes()));  

  19.         System.out.println(encode3(s.getBytes()));  

  20.   

  21.         //System.out.println(convertBinaryString2Int("10"));  

  22.     }  

  23.       

  24.     public static String encode(String str){  

  25.         char[] base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();   

  26.         char[] cs = str.toCharArray();  

  27.         StringBuilder sb = new StringBuilder();  

  28.         for(char c: cs){  

  29.             String bin = Integer.toBinaryString((int)c);  

  30.             System.out.println("bin: "+bin);  

  31.             while(bin.length()<8){  

  32.                 bin = "0"+bin;  

  33.             }  

  34.             sb.append(bin);  

  35.         }  

  36.         System.out.println("sb: "+sb);  

  37.         List<String> list = new ArrayList<String>();  

  38.         String temp = sb.toString();  

  39.         while(temp.length()>6){  

  40.             String s  = temp.substring(0,6);  

  41.             temp = temp.substring(6,temp.length());  

  42.             list.add(s);  

  43.         }  

  44.         while(temp.length()<6){  

  45.             temp+="0";  

  46.         }  

  47.         list.add(temp);  

  48.         String result = "";  

  49.         for(String s:list){  

  50.             //System.out.println(s);  

  51.             int num = convertBinaryString2Int(s);  

  52.             System.out.print(num+" ");  

  53.             result += base[num]+"";  

  54.                       

  55.         }  

  56.         System.out.println();  

  57.         if(result.length()%3==1){  

  58.             result+="==";  

  59.         }  

  60.         if(result.length()%3==2){  

  61.             result+="=";  

  62.         }  

  63.         return result;  

  64.     }  

  65.   

  66.     public static int convertBinaryString2Int(String str){  

  67.         int sum = 0;  

  68.         for(int i= str.length()-1;i>=0;i--){  

  69.             Integer num = Integer.parseInt(str.charAt(i)+"");  

  70.             sum += num * Math.pow(2, str.length()-1-i);   

  71.         }  

  72.         return sum;  

  73.     }  

  74.       

  75.     /**   

  76.         * 编码   

  77.         * @param bstr   

  78.         * @return String   

  79.         */      

  80.        public static String encode(byte[] bstr){      

  81.        return new sun.misc.BASE64Encoder().encode(bstr);      

  82.        }    

  83.          

  84.        /**  

  85.          * 二进制数据编码为BASE64字符串  

  86.          *  

  87.          * @param bytes  

  88.          * @return  

  89.          * @throws Exception  

  90.          */    

  91.         public static String encode2(final byte[] bytes) {    

  92.             return new String(Base64.encodeBase64(bytes));    

  93.         }    

  94.           

  95.         public static String encode3(final byte[] bytes) {    

  96.             return new String(java.util.Base64.getEncoder().encode(bytes));    

  97.         }    

  98. }  



获取【下载地址】   QQ: 313596790   【免费支持更新】
支持三大数据库 mysql  oracle  sqlsever   更专业、更强悍、适合不同用户群体
新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统

你可能感兴趣的:(java,base64)