Android uploads text files to the server via HttpPost.

Source: Internet
Author: User
Tags log log

/** * 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.