美文网首页
2020-06-29【Java:Base64编码解码】

2020-06-29【Java:Base64编码解码】

作者: skillplus | 来源:发表于2020-06-29 13:57 被阅读0次

    Base64编码解码已经加入Java 8 类库的标准。

    参考文档地址:https://www.runoob.com/java/java8-base64.html

    public class Test {
      public static void main(String[] args) throws Exception {
        String str = "锄禾日当午,汗滴禾下土。\n谁知盘中餐,粒粒皆辛苦。";
        
        String base64_1 = Base64.getEncoder().encodeToString(str.getBytes("utf-8"));
        System.out.println(base64_1);
        System.out.println("*******");
        
        String base64_2 = Base64.getMimeEncoder().encodeToString(str.getBytes("utf-8"));
        System.out.println(base64_2);
        System.out.println("*******");
    
        Decoder decoder = Base64.getMimeDecoder();
        
        byte[] bs_1 = decoder.decode(base64_1);
        System.out.println(new String(bs_1, "utf-8"));
        System.out.println("*******");
        
        byte[] bs_2 = decoder.decode(base64_2);
        System.out.println(new String(bs_2, "utf-8"));
      }
    }
    

    相关文章

      网友评论

          本文标题:2020-06-29【Java:Base64编码解码】

          本文链接:https://www.haomeiwen.com/subject/ncpofktx.html