PHP side Receive code:
Public functionGet_file () {$local _path= "./public/daixu_picture/figure/";//storage path for server files $img _name=basename($_files[' UploadedFile '] [' name ']);//the image name in the server (UploadedFile is the key value name, can be set by itself) $target _path=$local _path.$img _name; $result=Move_uploaded_file($_files[' UploadedFile '] [' Tmp_name '],$target _path); if($result) { Echo"Upload succeeded"; } Else{ Echo"Upload Failed"; } }
Android Side code:
/** Function: Upload image and Carry parameters * Incoming parameter: Http_url (server Destination address), filepath (address of native picture)*/ Public voidUploadimage (FinalString Http_url,FinalString filepath) { NewThread (NewRunnable () {@Override Public voidrun () {Try{File File=NewFile (filepath); if(!file.exists ()) {LOG.I ("Error", "file does not exist"); } HttpClient Client=Newdefaulthttpclient (); HttpPost Post=NewHttpPost (Http_url); Filebody Filebody=NewFilebody (file, "Image/jpeg"); Multipartentity Entity=Newmultipartentity (); Entity.addpart ("UploadedFile", filebody);//UploadedFile is the name of the key value for the image uploadEntity.addpart ("Key_app",NewStringbody ("1"));//sets the parameter to pass in, Key_app is the key value namepost.setentity (entity); HttpResponse Response=Client.execute (POST); if(Response.getstatusline (). Getstatuscode () = = 200) {httpentity httpentity=response.getentity (); String result= Entityutils.tostring (httpentity, "Utf-8"); LOG.E ("Returned results", result); } }Catch(Exception e) {e.printstacktrace (); }}). Start (); }
Android uploads images and adds parameters to PHP receive