java URL友好的Base64编码工具类

java URL友好的Base64编码工具类

package com.pro.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * Base64 工具类
 */
public class Base64Utils {

    private static Logger logger = LoggerFactory.getLogger(Base64Utils.class);

    /**
     * 标准的 Base64 编码
     *
     * @param content 需要编码的内容
     * @return
     */
    public static String encode(String content) {
        if (content == null) {
            logger.warn(">>> Base64 编码内容为null!");
            return null;
        }

        try {
            return Base64.getEncoder().encodeToString(content.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            logger.error(">>> Base64 编码异常!", e);
        }
        return null;
    }

    /**
     * 标准的 Base64 解码
     *
     * @param content 需要解码的内容
     * @return
     */
    public static String decode(String content) {
        if (content == null) {
            logger.warn(">>> Base64 解码内容为null!");
            return null;
        }

        try {
            byte[] decode = Base64.getDecoder().decode(content.getBytes(StandardCharsets.UTF_8));
            return new String(decode, StandardCharsets.UTF_8);
        } catch (Exception e) {
            logger.error(">>> Base64 编码异常!", e);
        }
        return null;
    }

    /**
     * URL 友好的 Base64 编码
     * (并非标准的Base64编码, 编码出来的内容无法用标准的Base64来解码, 要使用URL安全的Base64来解码)
     * URL 友好型的 Base64 编码, 它将Base64编码中的 "+/" 使用 "-_" 来代替, 并且不在末尾追加 =
     *
     * @param content 需要编码的内容
     * @return
     */
    public static String urlSafeEncode(String content) {
        // 标准的Base64编码
        String s = encode(content);
        if (s == null) return null;

        // URL 友好型的 Base64 编码, 它将Base64编码中的 "+/" 使用 "-_" 来代替, 并且不在末尾追加 =
        return s.replace("+", "-")
                .replace("/", "_")
                .replace("=", "");
    }

    /**
     * URL 友好的 Base64 解码
     * (可解码标准的Base64编码内容)
     * 因为标准的Base64编码中不包含 "-_", 所以这个方法也能解码标准的Base64编码
     *
     * @param content 需要解码的内容
     * @return
     */
    public static String urlSafeDecode(String content) {
        if (content == null) {
            logger.info(">>> URL安全的 Base64 解码内容为null!");
            return null;
        }

        /**
         * 因为 URL 友好型的 Base64 编码, 它将Base64编码中的 "+/" 使用 "-_" 来代替, 并且不在末尾追加 =
         * 所以将非标准的Base64编码里面的 "-_" 替换成 "+/", 还原成标准的Base64编码
         * 因为标准的Base64编码中不包含 "-_", 所以这个方法也能解码标准的Base64编码
         */
        String s = content.replace("-", "+")
                .replace("_", "/");
        return decode(s);
    }


    public static void main(String[] args) {
        String txt = "{\"name\":\"S_GUID\",\"first_value\":\"5fb3a8e544be422\",\"value\":\"测试中文123test\"}";

        String encode = encode(txt);
        System.out.println("编码后的内容: " + encode);

        String decode = decode(encode);
        System.out.println("解码前的内容: " + txt);
        System.out.println("解码后的内容: " + decode);
        System.out.println("编码后是否相等: " + txt.equals(decode));
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值