Base64Util工具类

public class Base64Util {
    /**
     * 编码UTF_8
     */
    public static final String CODING_UTF_8 = "UTF-8";

    /**
     * 编码GB2312
     */
    public static final String CODING_GB2312 = "GB2312";

    /**
     *
     * @方法名:encode
     * @方法描述【方法功能描述】base64编码并指定文本编码
     * @param s 待编码字符串
     * @param charset 字符串编码
     * @return 编码后字符串
     */
    public static String encode(String s, String charset) throws UnsupportedEncodingException {
        return new String(Base64.encodeBase64(s.getBytes(charset)));
    }

    /**
     *
     * @方法名:encode
     * @方法描述【方法功能描述】base64编码
     * @param s 待编码字符串
     * @return 编码后字符串
     */
    public static String encode(String s) {
        return new String(Base64.encodeBase64(s.getBytes()));
    }

    /**
     *
     * @方法名:decode
     * @方法描述【方法功能描述】base64解码
     * @param s 待解码字符串
     * @return 解码后字符串
     */
    public static String decode(String s) {
        return new String(Base64.decodeBase64(s.getBytes()));
    }

/**
     *
     * @方法名:decode
     * @方法描述【方法功能描述】base64解码防止中文乱码
     * @param s 待解码字符串
     * @return 解码后字符串
     */
    public static String decodeUTF8(String s) {
        return new String(Base64.decodeBase64(s),"utf-8");
    }



    public static void main(String[] args) {
        try {
            System.out.println(Base64Util.encode("cc", Base64Util.CODING_UTF_8));
            System.out.println(Base64Util.decode("Y2M="));
        }
        catch (UnsupportedEncodingException e) {
            System.out.println(e);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值