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