PackageCom.education.util;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder;ImportJava.io.*;/*** Created by Jn-dinggao on 2016/2/4.*/ Public classBase64util {//Converts a picture file into a byte array string and Base64 it for encoding Public Staticstring Getimagestr (String imgfilepath) {byte[] data =NULL; //reading a picture byte array Try{InputStream in=NewFileInputStream (Imgfilepath); Data=New byte[In.available ()]; In.read (data); In.close (); } Catch(IOException e) {e.printstacktrace (); } //Base64 encoding of byte arraysBase64encoder encoder =NewBase64encoder (); returnEncoder.encode (data);//returns a BASE64 encoded byte array string } /*** Base64 transcoding image * IMGSTR base64 code * Imgfilepath Picture Save path + name *@paramImgstr *@paramImgfilepath *@return */ Public Static Booleangenerateimage (String imgstr, String imgfilepath) {if(Imgstr = =NULL)//image data is empty return false; Base64decoder Decoder=NewBase64decoder (); Try { //Base64 decoding byte[] bytes =Decoder.decodebuffer (IMGSTR); for(inti = 0; i < bytes.length; ++i) {if(Bytes[i] < 0) {//Adjust Exception DataBytes[i] + = 256; } } //Create a JPEG pictureOutputStream out =NewFileOutputStream (Imgfilepath); Out.write (bytes); Out.flush (); Out.close (); return true; } Catch(Exception e) {return false; } }}
BASE64 encoded image and base64 encoding for mutual transfer