Encode the image with Base64,

Source: Internet
Author: User

Encode the image with Base64,

From http://commons.apache.org/proper/commons-codec/download_codec.cgi

 

Download commons-codec-1.9-bin.zip unzip get the commons-codec-1.9.jar to add the jar package to the Java project created by eclipse, You can reference the classes provided in the jar package.

 

Public interface BinaryDecoder extends Decoder interface can also inherit the parent interface

Base64.java source code path:

Commons-codec-1.9-src \ src \ main \ java \ org \ apache \ commons \ codec \ binary \ Base64.java

 

Because Base64 directly operates byte streams, rather than bytes streams, this class is thread-safe.

The standard encoding table consists of 26 uppercase letters, 26 lowercase letters, 0-9 +/64 characters in total.

 

Import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import javax. imageio. stream. fileImageOutputStream; import org. apache. commons. codec. binary. base64; public class Base64Demo_01 {public static void main (String [] args) throws FileNotFoundException {// convert the image file to a byte array, encode it with Base64: File = new file ("c: \ a.jpg"); FileInputStream fin = null; byte [] data = null; try {fin = new FileInputStream (file); data = new byte [fin. available ()]; fin. read (data); fin. close ();} catch (Exception e) {e. printStackTrace ();} // view the encoding and generate the String encodeStr = Base64.encodeBase64String (data); System. out. println (encodeStr); File outFile = new File ("c: \ save.txt"); FileOutputStream fos = new FileOutputStream (outFile); try {fos. write (encodeStr. getBytes (); fos. flush (); fos. close ();} catch (IOException e) {e. printStackTrace ();} // byte encoding byte [] encodeImage = Base64.encodeBase64 (data); // generate the encoded jpg image File imgFile = new File ("c: \ B .jpg "); try {FileImageOutputStream fos1 = new FileImageOutputStream (imgFile); fos1.write (encodeImage); fos1.flush (); fos1.close ();} catch (IOException e) {e. printStackTrace ();} System. out. println ();}}



Related Article

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.