使用jdk自带jar
package demo;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64EncodeAndDecode {
public static void main(String[] args) {
String temp = "你好,我叫tom";
BASE64Encoder encoder = new BASE64Encoder();
BASE64Decoder decoder = new BASE64Decoder();
try {
temp = encoder.encode(temp.getBytes("utf-8"));
System.out.println(temp);
byte[] tempByte = decoder.decodeBuffer(temp);
temp = new String(tempByte, "utf-8");
System.out.println(temp);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}