Java Base64转码

参考代码:

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

public class Test {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "hello world";
        byte[] b = str.getBytes("utf-8");
        String encodeStr = new String(Base64.getEncoder().encode(b), "utf-8");
        String decodeStr = new String(Base64.getDecoder().decode(encodeStr), "utf-8");
        System.out.println("origin string=>" + str);
        System.out.println("base64 string=>" + encodeStr);
        System.out.println("decode string=>" + decodeStr);
        //output:
        // origin string=>hello world
        // base64 string=>aGVsbG8gd29ybGQ=
        // decode string=>hello world

    }
}



标签: base64、encodestr、string、decode、unsupportedencodingexception、面试
  • 回复
隐藏