Java学习笔记-封装Java Util包Base64方法

这是一个Java程序员wuwenchao创建的Base64工具类,用于简化开发过程中的Base64编码和解码操作。该类静态初始化了Base64的编码器和解码器,并提供了针对byte数组和字符串的编码与解码方法。
摘要由CSDN通过智能技术生成

懒人菜鸟入门Java系列-习惯性封装常用方法,方便开发过程中调用

  注释: Java版本-1.8

* @Author wuwenchao
 * @Version 1.0.0
 * @Date 2022/4/29 10:07
 */

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * 封装Java Util包Base64方法
 *
 * @author wuwenchao
 * @Date 2022-04-29 10:07
 **/
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();
    }

    /**
     * byte[] Base64编码
     *
     * @Author wuwenchao
     * @Date 2022/4/29 10:11
     */
    public static byte[] encodeBase64(byte[] bytes) {
        return encoder.encode(bytes);
    }
    /**
     * 字符串 Base64编码
     *
     * @Author wuwenchao
     * @Date 2022/4/29 10:11
     */
    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;
    }
    /**
     * byte[] Base64编码为 字符串
     *
     * @Author wuwenchao
     * @Date 2022/4/29 10:11
     */
    public static String encodeBase64String(byte[] bytes) {
        return encoder.encodeToString(bytes);
    }
    /**
     * 字符串 Base64编码为 byte[]
     *
     * @Author wuwenchao
     * @Date 2022/4/29 10:11
     */
    public static byte[] encodeBase64Byte(String source) {
        byte[] bytes = encodeBase64(source.getBytes());
        return  bytes;
    }
    /**
     * Base64Byte 解码
     *
     * @Author wuwenchao
     * @Date 2022/4/29 10:11
     */
    public static byte[] decodeBase64(byte[] bytes) {
        return decoder.decode(bytes);
    }
    /**
     * Base64字符串 解码为 byte[]
     *
     * @Author wuwenchao
     * @Date 2022/4/29 10:11
     */
    public static byte[] decodeBase64Byte(String string) {
        return decoder.decode(string.getBytes());
    }
    /**
     * Base64Byte 解码为字符串
     *
     * @Author wuwenchao
     * @Date 2022/4/29 10:11
     */
    public static String decodeBase64String(byte[] bytes) {
        try {
            return new String(decoder.decode(bytes),encodingUTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * Base64字符串 解码
     *
     * @Author wuwenchao
     * @Date 2022/4/29 10:11
     */
    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;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值