Encoding as Base64 in Java

前端 未结 17 2767
失恋的感觉
失恋的感觉 2020-11-21 13:44

I need to encode some data in the Base64 encoding in Java. How do I do that? What is the name of the class that provides a Base64 encoder?


I tried to use the <

17条回答
  •  野的像风
    2020-11-21 14:11

    Google Guava is another choice to encode and decode Base64 data:

    POM configuration:

    
       guava
       com.google.guava
       jar
       14.0.1
    
    

    Sample code:

    String inputContent = "Hello Việt Nam";
    String base64String = BaseEncoding.base64().encode(inputContent.getBytes("UTF-8"));
    
    // Decode
    System.out.println("Base64:" + base64String); // SGVsbG8gVmnhu4d0IE5hbQ==
    byte[] contentInBytes = BaseEncoding.base64().decode(base64String);
    System.out.println("Source content: " + new String(contentInBytes, "UTF-8")); // Hello Việt Nam
    

提交回复
热议问题