Using BASE64 in Java

4

Problem:

I can not import the classes BASE64Decoder and BASE64Encoder , there are N code that use them and when I use them everything that is related does not work just because they are not found.

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

I need to understand why I can not find the sun.misc package and the BASE64Decoder and BASE64Encoder classes. Who can help?

    
asked by anonymous 27.02.2014 / 16:25

1 answer

5

Since Java 6, you can use static methods printBase64Binary and parseBase64Binary of class DatatypeConverter .

See an example:

String base64 = DatatypeConverter.printBase64Binary("Blá = 1".getBytes("UTF-8"));
System.out.println(base64);

String original = new String(DatatypeConverter.parseBase64Binary(base64), "UTF-8");
System.out.println(original);

This will print:

  

QmzDoSA9IDE =

     

Blah = 1

    
27.02.2014 / 17:05