BASE64 encoded image and base64 encoding for mutual transfer

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.