JS code//upload with Formdata
1 varDataurl = $ (' #canvas '). Getdataurl ();2 varimg = $ (' '). attr (' src ', dataurl);//Show Pictures3 varOmyform =NewFormData ();//Create Formdata4 varBlobbin = Dataurltoblob (Dataurl);//Base64 converting BLOBs5Omyform.append ("Blobobject", Blobbin);//want to formdata add BLOB data6 $.ajax ({7URL: $.geturl () + "activity/updateimg",8Type: "POST",9 Data:omyform,TenAsyncfalse, OneCachefalse, AContentType:false, -ProcessData:false, -Successfunction(msg) { theConsole.log ("Yes"); - } - }); - + //**dataurl to blob** dataurl convert blob - functionDataurltoblob (dataurl) { + vararr = Dataurl.split (', '), MIME = Arr[0].match (/:(. *); [1], ABSTR = Atob (arr[1]), n = bstr.length, U8arr =NewUint8array (n); at while(n--) { -U8arr[n] =bstr.charcodeat (n); - } - return NewBlob ([U8arr], {type:mime}); -}
Java Code//spring MVC
1@RequestMapping (value= "updateimg", method=requestmethod.post)2 Public voidUpLoad (httpservletrequest request,httpservletresponse response)throwsIOException {3System.out.println ("Enter ...");4 //getting the file information from the request needs to convert the request to the Multiparthttpservletrequest type5Multiparthttpservletrequest mulrequest = RequestinstanceofMultiparthttpservletrequest? (multiparthttpservletrequest) Request:NULL;6Iterator<string> FileNames =mulrequest.getfilenames ();7 byte[] Imgbyte =NULL;8 if(Filenames.hasnext ()) {//traverse the picture information in the request9String fileName = Filenames.next ();//the name of the parameter that corresponds to the pictureTenMultipartfile file = Mulrequest.getfile (fileName);//Get to Picture One if(File! =NULL) { ASystem.out.println ("File.getsize ():" + file.getsize ());//Picture Size -Imgbyte=file.getbytes ();//can get an array of bytes to the picture - } the } - - for(inti=0;ii) - { + if(imgbyte[i]<0) -{//Adjust Exception Data +imgbyte[i]+=256; A } at } - //Create a JPEG picture -String Imgfilepath = "D://222.jpg";//the newly generated picture -OutputStream out =NewFileOutputStream (imgfilepath); - Out.write (imgbyte); - Out.flush (); in out.close (); -}
Ajax Java Base64 picture storage