base64 java decode code example

Example 1: Java use Base64

//For Strings:
  
  //Encode:
  public static String encode(String input) {

      return Base64.getEncoder().encodeToString(input.getBytes());

  }

  //Decode:
  public static String decode(String input) {

      byte[] decodedBytes = Base64.getDecoder().decode(input);
      return new String(decodedBytes);

  }
  
//For Urls
  //Encode:
  public static String encodeURL(String input) {

      return Base64.getUrlEncoder().encodeToString(input.getBytes());

  }

  //Decode:
  public static String decodeURL(String input) {

      byte[] decodedBytes = Base64.getUrlDecoder().decode(input);
      return new String(decodedBytes);

  }

Example 2: base64.decode android

// Sending side
byte[] data = text.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);

// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");

Tags:

Java Example