Android uploads text files to the server's instance code via HttpPost _android

Source: Internet
Author: User
Tags log log stringbuffer

No more nonsense to say, directly to everyone to paste the key code.

/** * Upload text to the server such as log log * @param urlstr request URL * @param uploadfile log log path */mnt/shell/emulated/0/log/log.log * @par Am NewName Log Name LOG.log * @return * * public static void HttpPost (activity activity,string urlstr,string Uploadfile,str
ing NewName) {logutil.info ("Getehttppostt", "urlstr= +urlstr+"; uploadfile= "+uploadfile+"; newname= "+newname", "I");
String end = "\ r \ n";
String Twohyphens = "--"; String boundary = "* * *";//boundary identifier int time_out = 10*1000; 
Timeout time httpurlconnection con = null; 
DataOutputStream ds = null;
InputStream is = null; try {URL url = new URL (urlstr); con = (httpurlconnection) url.openconnection (); Con.setreadtimeout (time_out); con.setconn
Ecttimeout (time_out);
/* Allow input, Output, do not use cache */Con.setdoinput (TRUE);
Con.setdooutput (TRUE);
Con.setusecaches (FALSE); Set HTTP connection Properties Con.setrequestmethod ("POST");//Request Mode Con.setrequestproperty ("Connection", "keep-alive"); You can continue to send multiple copies of data in a single TCP connection without disconnecting Con.setrequestproperty ("Charset", "UTF-8");//Set Encoding Con.setrequestproperty ("Content-type",//multipart/form-data can upload the file encoding format "multipart/form-data;boundary=" + boundary);
ds = new DataOutputStream (Con.getoutputstream ());
Ds.writebytes (twohyphens + boundary + end);
Ds.writebytes ("Content-disposition:form-data" + "name=\" stblog\ "filename=\" "+ newName +" \ "+ end);
Ds.writebytes (end);
FileInputStream FileInputStream fstream = new FileInputStream (uploadfile) to obtain documents;
/* Set each write 1024bytes/int buffersize = 1024;
byte[] buffer = new Byte[buffersize];
int length =-1; /* Read data from file to 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);/ending Fstream.close ();
Ds.flush ();
/* Get response content * * = Con.getinputstream ();
int ch;
StringBuffer B = new StringBuffer (); while (ch = is.read ())!=-1) {b.append ((char) ch),}/* Displays response in Dialog/ShowDialog (activity,true,uploadfile, "Upload succeeded "+ b.tostring (). Trim ()); catch (Exception e) {ShowdiAlog (Activity,false,uploadfile, "upload failure" + 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 dialog method/private static void ShowDialog (final activity activity,final Boolean issuccess,final String upload File,final String mess) {Activity.runonuithread (new Runnable () {@Override public void run () {New Alertdialog.builder (AC
tivity). Settitle ("message"). Setmessage (Mess). Setnegativebutton (OK, new Dialoginterface.onclicklistener () { public void OnClick (Dialoginterface dialog, int which) {File File = new file (uploadfile); if (File.exists () &&issuc
Cess) {//log file exists and upload log successfully file.delete ();
Toast.maketext (activity, "Log log deleted", Toast.length_short). Show ();
). Show ();
}
}); }

The above content is small to introduce Android through HttpPost upload text files to the server instance code, the code is easy to understand, with comments, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.