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

Java Base64 Decode

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

Example #1 (decode string):

import java.util.Base64;

class Base64Decode {
  public static void main(String[] args) {
    String b64 = "Z3VydQ==";
    byte[] decoder = Base64.getDecoder().decode(b64);
    String str = new String(decoder);
    System.out.println(str); //-> "guru"
  }
}
Comments (4)

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

  • nicolas,
    Hi, for decode a PDF File its the same method?
    • Administrator,
      Hello,
      That’s right, you should use the same method, both for decoding text and for binary data. Just keep in mind that the decode() method returns a byte array. By the way, you can see a full example here: Convert Base64 to PDF in Java.
  • Gopal,
    Hi, I want to encode a Japanese language character from javascript and decode in java.

    Also in the same project, I want to encode the Japanese language character in java and decode in java.

    Means to say that I have to encode character from 2 sides there are 2 encoders one on js and other is from java but the decoder for both of them is same.

    I am using JSP and Struts2 action.

    Please help me I am just stuck in that project.

    Thanks in advance.
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.