Base64 usage, decoding, and encoding

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.