The previous article only talked about the foreground operation, this is specifically related to the Java background processing, the foreground through the AJAX submission will BASE64 encoded image data to the Java background, and then the Java side to receive processing, through the image data information Base64 decoding, The picture data information is then uploaded to the server for saving, and the path address of the picture is stored in the database. OK, no more nonsense, just paste the code.
1, the front desk JS code:
$.ajax ({async:false,//is asynchronous cache:false,//whether to use cached type: "POST", Data:{filedata:filedata,licencename:licencename,cust _tax_code:cust_tax_code,phonenum:phonenum,state_id:state_id},datatype: "JSON", Timeout:1000,contenttype: ' application/x-www-form-urlencoded; Charset=utf-8 ', url: $ (' #ctx '). Val () + "Customercheckservlet?action=uploadlicence", success:function (Result) { Console.log (Result), if (result = = True) {alert (' Success upload~~~ ');} else if (result = = False) {alert (' Error upload~~~ ');}},error:function () {alert ("Error linking~");}});
2. Background Java code
/** * Document Upload * @param request * @param response * @throws ioexception */public void uploadlicence (HttpServletRequest reques T,httpservletresponse response) throws Ioexception{log.info ("=====================uploadlicence");d f = new SimpleDateFormat ("Yyyy-mm-dd"); String Cust_tax_code = Request.getparameter ("Cust_tax_code"); String phonenum = Request.getparameter ("Phonenum"); String licencename = Request.getparameter ("Licencename"); String fileData = Request.getparameter ("FileData"),//BASE64 encoded picture data information, Base64 decoding byte array string imgpath = UploadFile ( Filedata,licenename);//File upload operation, upload to server (this is uploaded to the Server project folder) Boolean result = false;//Final upload success or not flag Custcheckinfo = new Customercheckinfo (); Custcheckinfo.setcust_tax_code (Cust_tax_code); Custcheckinfo.setphonenum (PhoneNum); Custcheckinfo.setupdate_time (Df.format (New Date ())); Boolean save_flag = Customerservice.saveregistcertinfo ( Custcheckinfo);//save path//Determine if the path exists in the database, and whether the file exists in the folder (the flag to determine if the upload is successful) Boolean is_success = Issuccessupload (Licencename, Cust_tax_code, PhOnenum); if (Save_flag && is_success) {result = true;} If the certificate upload succeeds, it is recorded in the record table if (result) {Staterecordinfo record = new Staterecordinfo (); Record.setcust_tax_code (Cust_tax_code ); Record.setphonenum (Phonenum); record.setstate_id (state_id); Savestaterecord (record);//execute state save operation} System.out.println ("===result:" +result); PrintWriter pw = Response.getwriter ();p w.print (Result);p w.close ();
/** * File Upload * @param fileData * @param fileName * @return */public string uploadfile (String filedata,string fileName) {//In its own Project to construct a folder for storing user photos string imgpath = This.getservletcontext (). Getrealpath ("/uploads/"); Create a file F = new If this folder does not exist File (Imgpath), if (!f.exists ()) {F.mkdir ();} Stitching the file name, does not exist create Imgpath = Imgpath + "/" + FileName + ". jpg"; f = new file (Imgpath); if (!f.exists ()) {F.mkdir ()} Log.info ("= = =" File saved location: "+imgpath);//use BASE64 to decode image file data Base64decoder decoder = new Base64decoder (); try {// Decrypt the image data into a byte array via Base64 decryption byte[] bytes = Decoder.decodebuffer (fileData);//construct byte array input stream Bytearrayinputstream Bais = new Bytearrayinputstream (bytes);//reads data from the input stream bufferedimage bi = imageio.read (Bais);//writes data information into the picture file Imageio.write (bi, "JPG", f);//regardless of the output format picture, there is no need to change bais.close ();} catch (IOException e) {log.error ("e:{}", E);} return Imgpath;}
/** * Determine if the upload was successful * @return */public boolean issuccessupload (String licencename,string cust_tax_code,string phonenum) { Boolean flag = false; String Licencepath = "";//the path saved after the upload of the certificate image custcheckinfo = Customerservice.getcustomercheckinfo (Cust_tax_code, Phonenum); Licencepath = Custcheckinfo.gettax_regist_cert ();//determine that the credential path is not empty and exists in the uploaded folder, indicating that the upload succeeded file F = new file ( Licencepath); if (Licencepath.length () >0 && f.exists ()) {flag = true;} return flag;}
All right, here it's all over, this is the Html5+jquery+canvas call camera to achieve the image upload the whole process of uploading, always feel that their ideas are somewhat chaotic, ah, slowly progress it!
Html5+canvas+jquery Call Mobile photo function implementation Image upload (ii)