Encode an image with base64

Source: Internet
Author: User

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 ();}}

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.