@File
2020-04-10T03:41:14.000000Z
字数 773
阅读 57
骚操作
public class Base64Tool {
private static BASE64Encoder base64Encoder;
private static BASE64Decoder base64Decoder;
static{
base64Encoder = new BASE64Encoder();
base64Decoder = new BASE64Decoder();
}
/**
* byte数组转换base64
* @param bytes byte 数组
* @return base64字符串
*/
public static String encode(byte[] bytes){
return base64Encoder.encode(bytes);
}
/**
* 字符串转换base64
* @param str 字符串
* @return base64字符串
*/
public static String encode(String str){
return encode(str.getBytes());
}
/**
* base64字符串转字符串
* @param str base64字符串
* @return 字符串
*/
public static String decode(String str){
return new String(decodeToByte(str));
}
/**
* base64字符串转byte数组
* @param str 字符串
* @return byte数组
*/
public static byte[] decodeToByte(String str){
byte[] bytes = null;
try {
bytes = base64Decoder.decodeBuffer(str);
} catch (IOException e) {
// e.printStackTrace();
}
return bytes;
}
}