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