Base64 解码 编码出现丢失末尾 “=”
测试代码:
import java.io.IOException;
import org.apache.tomcat.util.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class TT {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String str = "DTE5MTQ1OQ1lbmRvYmoNMyAwIG9iag08PCANL1R5cGUgL1BhZ2VzIA0vS2lkcyBbIDQgMCBSIF0g DS9Db3VudCAxDT4+DWVuZG9iag14cmVAATAgIDkNAAAwAAAwAAAwMCA2NTUzNSBAAQowAAAwAAAw AAE5IDAwAAAwIG4NCjAwAAAwAAAwNjkgAAAwAAAgbg0KAAAwAAE5MjEwNSAwAAAwMCBuDQowAAAw AAAwMTgyIDAwAAAwIG4NCjAwAAAwAAAzMzMgAAAwAAAgbg0KAAAwAAAwAAQxOSAwAAAwMCBuDQow AAAwAAAwNDM3IDAwAAAwIG4NCjAwAAAxOTIwODMgAAAwAAAgbg0KdHJhaWxlcg08PA0vU2l6ZSA5 DS9Sb290IDEgMCBSDS9JbmZvIDIgMCBSDS9JRFs8MjkwAAAwAAAyMzQ4AAAwMEZGMTgwAAAwRkY2 NzAwAAA+PDI5AAAwAAAwMjM0ODAwAABGRjE4AAAwMEZGNjcwAAAwPl0NPj4Nc3RhcnR4cmVAATE5 MjE2Nw0lJUVPRg0=";
// jdkBase64(str);
// apacheBase64(str);
test1(str);
test2(str);
test3(str);
test4(str);
}
/**
* jdk 解码编码
* @param str
* @throws Exception
*/
public static void test1(String str) throws Exception {
System.out.println("test1 ... [begin]");
jdkEncode(jdkDecode(str));
System.out.println("test1 ... [ end ]");
}
/**
* apache 解码编码
* @param str
*/
public static void test2(String str) throws Exception{
System.out.println("test2 ... [begin]");
apacheEncode(apacheDecode(str));
System.out.println("test2 ... [ end ]");
}
/**
* jdk 解码 apache 编码
* @param str
*/
public static void test3(String str) throws Exception{
System.out.println("test3 ... [begin]");
apacheEncode(jdkDecode(str));
System.out.println("test3 ... [ end ]");
}
/**
* apache 解码 jdk 编码
* @param str
*/
public static void test4(String str) throws Exception{
System.out.println("test4 ... [begin]");
jdkEncode(apacheDecode(str));
System.out.println("test4 ... [ end ]");
}
@SuppressWarnings("restriction")
public static void jdkBase64(String str) throws Exception {
System.out.println("jdk:"+str.substring(str.length()-10));
byte[] bytes = new BASE64Decoder().decodeBuffer(str);
str = new BASE64Encoder().encode(bytes);
System.out.println("jdk:"+str.substring(str.length()-10));
}
/**
* jdk 解码
* @param str
* @return
* @throws IOException
*/
@SuppressWarnings("restriction")
public static byte[] jdkDecode(String str) throws IOException {
System.out.println("jdk:"+str.substring(str.length()-10));
return new BASE64Decoder().decodeBuffer(str);
}
/**
* jdk 编码
* @param str
* @return
* @throws IOException
*/
@SuppressWarnings("restriction")
public static String jdkEncode(byte[] bytes) throws IOException {
String str = new BASE64Encoder().encode(bytes);
System.out.println("jdk:"+str.substring(str.length()-10));
return str;
}
/**
* apache 解码
* @param str
* @return
* @throws Exception
*/
public static byte[] apacheDecode(String str) throws Exception {
System.out.println("apa:"+str.substring(str.length()-10));
Base64 base64 = new Base64();
return base64.decode(str);
}
/**
* apache 编码
* @param bytes
* @return
* @throws Exception
*/
public static String apacheEncode(byte[] bytes) throws Exception {
Base64 base64 = new Base64();
String str = base64.encodeAsString(bytes);
System.out.println("apa:"+str.substring(str.length()-10));
return str;
}
public static void apacheBase64(String str) {
System.out.println("apache:"+str.substring(str.length()-10));
Base64 base64 = new Base64();
byte[] bytes = base64.decode(str);
str = base64.encodeAsString(bytes);
System.out.println("apache:"+str.substring(str.length()-10));
}
public static void zlcBase64(String str) throws Exception {
System.out.println("zlc:"+str.substring(str.length()-10));
byte[] bytes = new BASE64Decoder().decodeBuffer(str);
Base64 base64 = new Base64();
str = base64.encodeAsString(bytes);
System.out.println("apache:"+str.substring(str.length()-10));
}
}
不建议使用 JDK 自带的 BASE64 解码;
使用 apache 工具包中的BASE64工具类(不只这一种) org.apache.tomcat.util.codec.binary.Base64