Public classImgBase64 { Public Static voidMain (string[] args)//test {String strimg=Getimagestr (); System. out. println (STRIMG); } Public StaticString getimagestr ()//Picture goto base64 {//Picture pathString Imgfile ="D:\\users\\sys\\pictures\\1.png";//InputStreaminch=NULL; byte[] data =NULL; Try { inch=NewFileInputStream (Imgfile); Data=New byte[inch. Available ()]; inch. Read (data); inch. Close (); } Catch(IOException e) {e.printstacktrace (); } Base64encoder encoder =NewBase64encoder (); returnEncoder.encode (data);// } Public Staticboolean generateimage (String imgstr)//base string to picture {// if(Imgstr = =NULL)// return false; Base64decoder Decoder=NewBase64decoder (); Try { byte[] B =Decoder.decodebuffer (IMGSTR); for(intI=0; i<b.length;++i) {if(b[i]<0)//Correct the error character {b[i]+= the; } } String Imgfilepath ="d:\\222.jpg";//OutputStream out=NewFileOutputStream (Imgfilepath); out. Write (b); out. Flush (); out. Close (); return true; } Catch(Exception e) {return false; } }}
Java Picture Base64 Cross-transfer