Base64 code upload image Java background receive instance

Source: Internet
Author: User

Original address: http://blog.csdn.net/tonyfreak/article/details/72522855 idea

The foreground passes data:image/jpeg;base64, the beginning of the Base64 encoded string string, the background receives the string to first Base64 decoding. Decodebuffer (), converted to binary encoding, Then use the byte output stream FileOutputStream () to save the file to the specified directory.
Report the following error
Error parsing HTTP Request Header
Note:further occurrences of HTTP header parsing errors'll be logged at DEBUG level.
To modify the size of the Sever.xml,header under Tomcat
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8000" protocol="HTTP/1.1" redirectPort="8443" maxHttpHeaderSize="10485760"/>
Turn base64 encoding into binary

         //convert pictures with base64ImageFile = Imagefile.replaceall ("Data:image/jpeg;base64,", "" "); Base64decoder Decoder=NewBase64decoder (); //Base64 decoding                    byte[] Imagebyte =NULL; Try{imagebyte=Decoder.decodebuffer (ImageFile);  for(inti = 0; i < imagebyte.length; ++i) {if(Imagebyte[i] < 0) {//Adjust Exception DataImagebyte[i] + = 256; }                              }                          } Catch(Exception e) {e.printstacktrace (); }  

Turn binary into file

    //Generate file nameString files =NewSimpleDateFormat ("Yyyymmddhhmmsssss"). Format (NewDate ())+ (NewRandom (). Nextint (9000)% (9000-1000 + 1) + 1000)                + ". png"; //generate file pathString filename = Constant.upload_path +files; Try {            //Generating FilesFile ImageFile =NewFile (filename);               Imagefile.createnewfile (); if(!imagefile.exists ())                {Imagefile.createnewfile (); } outputstream ImageStream=NewFileOutputStream (ImageFile);                Imagestream.write (Imagebyte);                Imagestream.flush ();                            Imagestream.close (); } Catch(Exception e) {e.printstacktrace (); }

Base64encoder expiration warning, because it is Sun's internal API, and is not recommended for use because of the potential risk of late deletion, so it is appropriate to use COMMONS-CODEC instead.

Logo = Logo.replaceall ("Data:image/png;base64," "," ")          ; New Base64 ();         // Base64 decoding              byte NULL ;         Try {            = decoder.decode (logo);  

Base64 encoding upload picture Java background receive instance

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.