A virtual teacher who reveals to you the great secrets of Base64

Java Base64 Encode

Since Java 1.8 developers can use the Base64.Encoder class to encode data to Base64. This class provides several methods for encoding byte data to Base64 using algorithms specified in RFC 4648 and RFC 2045. In addition, Java provides the Base64.Decoder class for decoding Base64 values to byte data.

Example #1 (encode string):

import java.util.Base64;

class Base64Encode {
  public static void main(String[] args) {
    String str = "guru";
    String b64 = Base64.getEncoder().encodeToString(str.getBytes());
    System.out.println(b64); //-> "Z3VydQ=="
Comments (6)

I hope you enjoy this discussion. In any case, I ask you to join it.

  • rohit,

    I would like to get the java code implementation to convert svg image to base-64.

    Can you share the code snippet
    • Administrator,
      Hi! Since SVG is just XML (i.e. text data), the snippet on this page does exactly what you want.
      • rohit,
        Above code snippet works only when you extract buffered image from svg file and convert that to string and providing this string as input.

        I would like to see code which converts svg file to buffered image.

        I tried so many ways using batik jars but none is giving right output

        Its been a week I am struggling. It will be helpful if you can provide me working sample from svg file to buffered image
  • Cora,
    Hi , and how could i encode PDF to Base64
  • Dev,
    How can we validate if the base64 String is corrupted in Java.
    Example String :
Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.