encode String to Base64 via java.util.Base64 - Java java.lang

Java examples for java.lang:String Base64

Description

encode String to Base64 via java.util.Base64

Demo Code


//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Base64;

public class Main {
    public static void main(String[] argv) throws Exception {
        String username = "java2s.com";
        String password = "java2s.com";
        System.out.println(encode(username, password));
    }//from w ww  .  jav  a2  s  .c  om

    public static String encode(String username, String password)
            throws UnsupportedEncodingException {
        byte[] encodedValue = null;
        encodedValue = Base64.getEncoder().encode(
                (username + ":" + password).getBytes(Charset
                        .forName("UTF-8")));
        return new String(encodedValue);
    }
}

Related Tutorials