/** * Upload text to the server such as log log * @param the URL of the URLSTR request* @param uploadfile log path */mnt/shell/emulated/0/log/log.log * @param the name of the NewName log LOG.log * @return*/ Public Static voidHttpPost (Activity activity,string urlstr,string uploadfile,string newName) {logutil.info ("Getehttppostt","urlstr="+urlstr+"; uploadfile="+uploadfile+"; Newname="+newname,"I"); String End="\ r \ n"; String Twohyphens="--"; String Boundary="*****";//Boundary Identification intTime_out =Ten* +;//Timeout periodHttpURLConnection con =NULL; DataOutputStream DS=NULL; InputStream is=NULL; Try{URL URL=NewURL (URLSTR); Con=(HttpURLConnection) url.openconnection (); Con.setreadtimeout (time_out); Con.setconnecttimeout (time_out); /*allow input, Output, do not use cache*/Con.setdoinput (true); Con.setdooutput (true); Con.setusecaches (false); //setting HTTP connection PropertiesCon.setrequestmethod ("POST");//Request MethodCon.setrequestproperty ("Connection","keep-alive");//can continuously send multiple copies of data in a single TCP connection without disconnectingCon.setrequestproperty ("Charset","UTF-8");//Set EncodingCon.setrequestproperty ("Content-type",//Multipart/form-data can upload the file encoding format "multipart/form-data;boundary="+boundary); DS=NewDataOutputStream (Con.getoutputstream ()); Ds.writebytes (Twohyphens+ Boundary +end); Ds.writebytes ("Content-disposition:form-data;"+"name=\ "stblog\"; filename=\ ""+ NewName +"\""+end); Ds.writebytes (end); //get the FileInputStream of the documentFileInputStream FStream =NewFileInputStream (UploadFile); /*set 1024bytes per write*/ intBufferSize =1024x768; byte[] buffer =New byte[buffersize]; intLength =-1; /*reading data from a file to a buffer*/ while(length = fstream.read (buffer))! =-1) { /*Write data to DataOutputStream*/ds.write (buffer,0, length); } ds.writebytes (end); Ds.writebytes (Twohyphens+ boundary + Twohyphens + end);//EndFstream.close (); Ds.flush (); /*Get Response Content*/ is=Con.getinputstream (); intch; StringBuffer b=NewStringBuffer (); while(ch = is. read ())! =-1) {b.append (Char) ch); } /*Show Response on dialog*/ShowDialog (activity,true, UploadFile,"Upload Successful"+b.tostring (). Trim ()); } Catch(Exception e) {showDialog (activity,false, UploadFile,"Upload failed"+e); }finally { /*Close DataOutputStream*/ if(ds!=NULL){ Try{ds.close (); } Catch(IOException e) {e.printstacktrace (); } } if( is!=NULL) { Try { is. Close (); } Catch(IOException e) {e.printstacktrace (); } } if(Con! =NULL) {con.disconnect (); } } } /*Show the method of dialog*/ Private Static voidShowDialog (Final Activity activity,final Boolean issuccess,final string uploadfile,final string mess) {Activi Ty.runonuithread (NewRunnable () {@Override Public voidrun () {NewAlertdialog.builder (activity). Settitle ("Message"). Setmessage (Mess). Setnegativebutton ("Determine",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intwhich) {File File=NewFile (UploadFile); if(File.exists () &&issuccess) {//log file exists and upload log is successfulFile.delete (); Toast.maketext (activity,"Log log deleted", Toast.length_short). Show (); }}). Show (); } }); }
Android uploads text files to the server via HttpPost.