一下是参考链接地址:
http://ghostfromheaven.iteye.com/blog/1611551
https://www.cnblogs.com/lukyw/archive/2012/09/12/2682102.html
static final byte[] BASE64_TABLE = { 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '+', '/' };
private static final byte BASE64_PAD = '=';
public static String encodeBase64ToUTF8(byte[] bytes) {
StringBuilder sb = new StringBuilder();
int idx = 0;
final int end = bytes.length;
for (; idx < end - 2; idx += 3) {
int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8
| bytes[idx + 2] & 0xff;
sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]);
sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]);
sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]);
sb.append((char) BASE64_TABLE[data & 0x3f]);
}
if (idx == end - 2) {
int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8;
sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]);
sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]);
sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]);
sb.append((char) BASE64_PAD);
} else if (idx == end - 1) {
int data = (bytes[idx] & 0xff) << 16;
sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]);
sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]);
sb.append((char) BASE64_PAD);
sb.append((char) BASE64_PAD);
}
return sb.toString();
}
private static String encodeString(String value){
String newValue = "";
if(value == null) return null;
try{
byte[] bytes = value.getBytes("UTF-8");
newValue = encodeB(bytes);
newValue = "=?utf-8?B?"+newValue+"?=";
}catch(java.io.UnsupportedEncodingException e){
newValue = value;
e.printStackTrace();
}
return newValue;
}