jdk8-Base64工具类

项目中涉及到 base64转码,使用jdk原生类处理编码转换

工具类代码


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

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

/**
 * Base64工具类
 *
 * @author z.y.l
 * @version v1.0
 * @date 2021/12/16
 */
public class Base64Util {
    private static final Logger logger = LoggerFactory.getLogger(Base64Util.class);

    public static String encode(String content){
        return encode(true,content,StandardCharsets.UTF_8);
    }
    public static String encodeMime(String content){
        return encode(false,content,StandardCharsets.UTF_8);
    }
    public static String encodeGbk(String content){
        return encode(true,content,Charset.forName("GBK"));
    }
    public static String decode(String content){
        return decode(true,content,StandardCharsets.UTF_8);
    }
    public static String decodeGbk(String content){
        return decode(true,content,Charset.forName("GBK"));
    }
    public static String decodeMime(String content){
        return decode(false,content,StandardCharsets.UTF_8);
    }
    public static String encodeBytes(byte[] content){
        return encode(true,content,StandardCharsets.UTF_8);
    }
    public static byte[] decodeBytes(String content){
        return decodeBytes(true,content,StandardCharsets.UTF_8);
    }
    public static String encode(boolean isBasic, String content, Charset charset){
        if( null == content || "".equals(content.trim()) ){ return null; }
        try{
            return encode(isBasic,content.getBytes(charset),charset);
        }catch (Exception e){
            logger.error("base64编码字符>字符失败",e);
        }
        return null;
    }
    public static String encode(boolean isBasic,byte[] content,Charset charset){
        if( null == content || content.length <= 0 ){ return null; }
        try{
            Base64.Encoder encoder = isBasic ? Base64.getEncoder() : Base64.getMimeEncoder();
            return new String(encoder.encode(content), charset);
        }catch (Exception e){
            logger.error("base64编码字节>字符失败",e);
        }
        return null;
    }
    public static String decode(boolean isBasic,String content,Charset charset){
        if( null == content || "".equals(content.trim()) ){ return null; }
        try{
            return new String(decodeBytes(isBasic,content,charset),charset);
        }catch (Exception e){
            logger.error("base64解码字符>字符失败",e);
        }
        return null;
    }
    public static byte[] decodeBytes(boolean isBasic,String content,Charset charset){
        if( null == content || "".equals(content.trim()) ){ return null; }
        try{
            return decodeBytes(isBasic,content.getBytes(charset));
        }catch (Exception e){
            logger.error("base64解码字符>字节失败",e);
        }
        return null;
    }
    public static byte[] decodeBytes(boolean isBasic,byte[] content){
        if( null == content || content.length <= 0 ){ return null; }
        try{
            Base64.Decoder decoder = isBasic ? Base64.getDecoder() : Base64.getMimeDecoder();
            return decoder.decode(content);
        }catch (Exception e){
            logger.error("base64编码字节>字节失败",e);
        }
        return null;
    }
}

测试代码

public class TestMain {
    public static void main(String[] args) {
        String test = "测试123213";
        String val = Base64Util.encode(test);
        System.out.println(val);
        System.out.println(Base64Util.decode(val));
    }
}

在这里插入图片描述

你可能感兴趣的:(springboot,工具,Java,java,spring,开发语言,base64,idea)