What is base64 encoding?
- It is a way of encoding/decoding the binary data.
- It represents binary data in ASCII format.
What are the benefits of base64 encoding?
-
It is used in email systems,As most of the email system allows transmission 6to 7 bits. Base 64 encoding allows 8bit data to transmit in these systems.
What is apache common codec?
This library provides base64 encoding and decoding.
The download link :-
http://commons.apache.org/codec/download_codec.cgiDemo Details:-
Encode an image to ASCII string and then reproducing it in another image.here the obj.jpg encoded to base64 ascii string, then through apache io util converted to obj1.jpg.
package com.surabhi.base64;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
public class Base64Demo {
/**
* @param args
*/
public static void main(String[] args) {
File file = new File("D:\obj.jpg");
try {
byte buffer[]=FileUtils.readFileToByteArray(file);
String ascii=Base64.encodeBase64String(buffer);
System.out.println(ascii);
File file2=new File("D:\obj1.jpg");
FileOutputStream fos=new FileOutputStream(file2);
IOUtils.write(Base64.decodeBase64(ascii), fos);
fos.flush();
fos.close();
//FileUtils.readFileToByteArray(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}