java8 base64编码和解码

package com.oy;

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

import org.junit.Test;

public class demo04 {

    final Base64.Decoder decoder = Base64.getDecoder();
    final Base64.Encoder encoder = Base64.getEncoder();
    
    @Test
    public void testBase64() throws Exception {
        byte[] textByte = "admin:123".getBytes(StandardCharsets.UTF_8);
        String encodedText = encoder.encodeToString(textByte);//编码
        System.out.println(encodedText);//YWRtaW46MTIz
        System.out.println(new String(decoder.decode(encodedText), "UTF-8"));//解码
    }
}

猜你喜欢

转载自www.cnblogs.com/xy-ouyang/p/11451068.html