Package Com.yundongsports.arena.controller.basketballsite;import Sun.misc.base64decoder;import Sun.misc.base64encoder;import java.io.*;/** * Created by CF on 2016/12/23. */public class Test {String c= ""; /** * Convert string to Picture * @param imgstr * @return * */public static boolean generateimage (string imgstr,string im Gfile) throws Exception {///byte array string Base64 decoded and generate picture if (imgstr = = Null)//image data is empty return false; Base64decoder decoder = new Base64decoder (); Try {//Base64 decode byte[] b = Decoder.decodebuffer (imgstr); for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {//adjust exception data b[i] + = 256; }}//generate jpeg picture String Imgfilepath = "d:\\imgg\\c.jpg";//newly generated picture OutputS Tream out = new FileOutputStream (imgfilepath); Out.write (b); Out.flush (); Out.close (); Return true; } catch (EXception E) {throw e; }} public static string Getimagestr (string Imgfile) {//converts The picture file to a byte array string and Base64 encodes it//string imgfile = "d:\\111. JPG ";//picture to be processed inputstream in = null; byte[] data = null;//reads a picture byte array try {in = new FileInputStream (imgfile); data = new Byte[in.available ()]; In.read (data); In.close (); } catch (ioexception E) {e.printstacktrace (); }//BASE64 encoding of byte array Base64encoder encoder = new Base64encoder (); Return Encoder.encode (data);//returns Base64 encoded byte array string} public static void main (string[] Args) throws Exception { String Aa=getimagestr ("d:\\img\\a.png"); System.out.print (aa); Generateimage (aa, ""); }}
Conversion between Java IO stream picture and string