Java base64 is an open-source Java class library for encoding and decoding (encode/decode) base64 strings and data streams. Base64 is one of the most common encoding methods used to transmit 8-bit code on the network. It can be used as the transmission code for email or WebService attachments.
Usage:
Set the class library javabase64-1.2.jar to the compilation path.
Sample Code:
Base64 encoding for string type
String encoded = base64.encode ("Hello, world! ");
String-type base64 Decoding
String decoded = base64.decode (encoded );
Character encoding method
String encoded = base64.encode ("Hello, world! "," UTF-8 ");
String decoded = base64.decode (encoded, "UTF-8 ");
Encode the file:
If the file size is small, you can directly read the file to the memory for encoding.
Byte [] source =...; // load your data here
Byte [] encoded = base64.encode (source );
Byte [] decoded = base64.decode (encoded );
If the bulky parts are large, we recommend that you use stream:
Code example base64 encoding:
Inputstream = new fileinputstream ("source.jpg ");
Outputstream = new fileoutputstream ("encoded. b64 ");
Base64.encode (inputstream, outputstream );
Outputstream. Close ();
Inputstream. Close ();
Code example base64 decoding:
Inputstream = new fileinputstream ("encoded. b64 ");
Outputstream = new fileoutputstream ("decoded.jpg ");
Base64.decode (inputstream, outputstream );
Outputstream. Close ();
Inputstream. Close ();
Java base64 project URL: http://www.sauronsoftware.it/projects/javabase64/