懒人菜鸟入门Java系列-习惯性封装常用方法,方便开发过程中调用
注释: Java版本-1.8
* @Author wuwenchao
* @Version 1.0.0
* @Date 2022/4/29 10:07
*/
import java.io.UnsupportedEncodingException;
import java.util.Base64;
public class JavaBase64Utils {
public static final String encodingUTF_8 = "UTF-8";
public static Base64.Encoder encoder;
public static Base64.Decoder decoder;
static {
decoder = Base64.getDecoder();
encoder = Base64.getEncoder();
}
public static byte[] encodeBase64(byte[] bytes) {
return encoder.encode(bytes);
}
public static String encodeBase64(String source) {
byte[] bytes = encodeBase64(source.getBytes());
try {
return new String(bytes, encodingUTF_8);
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
return null;
}
public static String encodeBase64String(byte[] bytes) {
return encoder.encodeToString(bytes);
}
public static byte[] encodeBase64Byte(String source) {
byte[] bytes = encodeBase64(source.getBytes());
return bytes;
}
public static byte[] decodeBase64(byte[] bytes) {
return decoder.decode(bytes);
}
public static byte[] decodeBase64Byte(String string) {
return decoder.decode(string.getBytes());
}
public static String decodeBase64String(byte[] bytes) {
try {
return new String(decoder.decode(bytes),encodingUTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public static String decodeBase64(String string) {
byte[] decode = decodeBase64(string.getBytes());
try {
return new String(decode, encodingUTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}



