Directly on the code:
ImportJava.io.FileOutputStream; Importjava.io.IOException; ImportJava.io.InputStream; ImportJava.io.OutputStream; ImportSun.misc.BASE64Decoder; ImportSun.misc.BASE64Encoder; Public classBase64test { Public Static voidMain (string[] args) {String strimg=Getimagestr (); System.out.println (STRIMG); Generateimage (STRIMG); } //Convert images into base64 strings Public StaticString getimagestr () {//Converts a picture file into a byte array string and Base64 it for encodingString imgfile = "D:\\tupian\\a.jpg";//pictures to be processedInputStream in =NULL; byte[] data =NULL; //reading a picture byte array Try{ in=NewFileInputStream (Imgfile); 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 strings into pictures Public Static Booleangenerateimage (String imgstr) {//Base64 decoding a byte array string and generating a picture if(Imgstr = =NULL)//image data is empty return false; Base64decoder Decoder=NewBase64decoder (); Try { //Base64 decoding byte[] B =Decoder.decodebuffer (IMGSTR); for(inti=0;i<b.length;++i) {if(b[i]<0) {//Adjust Exception Datab[i]+=256; } } //Create a JPEG pictureString Imgfilepath = "D:\\tupian\\new.jpg";//the newly generated pictureOutputStream out =NewFileOutputStream (Imgfilepath); Out.write (b); Out.flush (); Out.close (); return true; } Catch(Exception e) {return false; } } }
Java background saves the Base64 string as a picture