前几天无意中看到Java中有Base64编码,不解的我去百科了一下,了解了Base64的基本使用和实现原理,于是在空暇时自己手动写了一个,这个类可以完成对字母数字的编码和解码工作,但是对于中文,还没有仔细研究其编码的实现过程。至于什么是Base64,用它来干什么,请移步到:http://zh.wikipedia.org/zh-cn/Base64
下面贴出这个工具类的源代码,供朋友们参考,本人新手写得不好,请用力拍砖:
/************************Base64Util.java*****************************/
1package util; 2 3import java.util.HashMap; 4import java.util.Map; 5 6/** 7 * This class is a tool class to convert Base64 string and 8 * normal string to each other. <br><br> 9 * More information about base64, Please refer to: 10 * http://zh.wikipedia.org/zh-cn/Base64 11 * @author user 12 */ 13public class Base64Util { 14 private static final Map<Integer, Character> base64CharMap = new HashMap<>(); 15 private static final String base64CharString = 16 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 17 static { 18 // initialize base64 map 19 for (int i=0;i<base64CharString.length();i++){ 20 char c = base64CharString.charAt(i); 21 base64CharMap.put(new Integer(i), new Character(c)); 22 } 23 } 24 25 /** 26 * This method is used to encode a normal string to base64 string 27 * @param origin 28 * The String to be encoded 29 * @return 30 * The String after encoded. 31 */ 32 public static String encode (String origin) { 33 if (origin == null) { 34 return null; 35 } 36 if (origin.length() == 0) { 37 return ""; 38 } 39 int length = origin.length(); 40 String binaryString = ""; 41 // to binary String 42 for (int i=0;i<length;i++) { 43 int ascii = origin.charAt(i); 44 String binaryCharString = Integer.toBinaryString(ascii); 45 while (binaryCharString.length() < 8) { 46 binaryCharString = "0" + binaryCharString; 47 } 48 binaryString += binaryCharString; 49 } 50 51 // to base64 index 52 int beginIndex = 0; 53 int endIndex = beginIndex + 6; 54 String base64BinaryString = ""; 55 String charString = ""; 56 while ((base64BinaryString = 57 binaryString.substring(beginIndex, endIndex)).length() > 0) { 58 //if length is less than 6, add "0". 59 while (base64BinaryString.length() < 6) { 60 base64BinaryString += "0"; 61 } 62 int index = Integer.parseInt(base64BinaryString, 2); 63 char base64Char = base64CharMap.get(index); 64 charString = charString + base64Char; 65 beginIndex += 6; 66 endIndex += 6; 67 if (endIndex >= binaryString.length()) { 68 endIndex = binaryString.length(); 69 } 70 if (endIndex < beginIndex) { 71 break; 72 } 73 } 74 if (length % 3 == 2) { 75 charString += "="; 76 } 77 if (length % 3 == 1) { 78 charString += "=="; 79 } 80 return charString; 81 } 82 83 /** 84 * This method is used to decode from base64 string to a normal string. 85 * @param encodedString 86 * The string to be decoded. 87 * @return 88 * The string after decoded. 89 */ 90 public static String decode(String encodedString) { 91 if (encodedString == null) { 92 return null; 93 } 94 if (encodedString.length() == 0) { 95 return ""; 96 } 97 //get origin base64 String 98 String origin = encodedString.substring(0,encodedString.indexOf("=")); 99 String equals = encodedString.substring(encodedString.indexOf("=")); 100 101 String binaryString = ""; 102 //convert base64 string to binary string 103 for (int i=0;i<origin.length();i++) { 104 char c = origin.charAt(i); 105 int ascii = base64CharString.indexOf(c); 106 String binaryCharString = Integer.toBinaryString(ascii); 107 while (binaryCharString.length() < 6) { 108 binaryCharString = "0" + binaryCharString; 109 } 110 binaryString += binaryCharString; 111 } 112 // the encoded string has 1 "=", means that the binary string has append 2 "0" 113 if (equals.length() == 1) { 114 binaryString = binaryString.substring(0,binaryString.length()-2); 115 } 116 // the encoded string has 2 "=", means that the binary string has append 4 "0" 117 if (equals.length() == 2) { 118 binaryString = binaryString.substring(0,binaryString.length()-4); 119 } 120 121 // convert to String 122 String charString = ""; 123 String resultString = ""; 124 int beginIndex = 0; 125 int endIndex = beginIndex + 8; 126 while ((charString = 127 binaryString.substring(beginIndex, endIndex)).length() == 8) { 128 int ascii = Integer.parseInt(charString, 2); 129 resultString += (char)ascii; 130 beginIndex += 8; 131 endIndex += 8; 132 if (endIndex > binaryString.length()) { 133 break; 134 } 135 } 136 return resultString; 137 } 138 139 // Test methods here. 140 public static void main(String[] args) { 141 String originString = 142 "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."; 143 String base64String = 144 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" + 145 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" + 146 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" + 147 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" + 148 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="; 149 150 System.out.println("===============encode================="); 151 String result = encode(originString); 152 System.out.println(result); 153 System.out.println(result.equals(base64String)); 154 System.out.println("===============decode================="); 155 String decodedString = decode(base64String); 156 System.out.println(decodedString); 157 System.out.println(originString.equals(decodedString)); 158 System.out.println(decode(encode("abcdefg"))); 159 } 160}