Mainactivity.java: Package com.xzq.upload; Import Java.io.DataOutputStream; Import Java.io.FileInputStream; Import Java.io.InputStream; Import java.net.HttpURLConnection; Import Java.net.URL; Import android.app.Activity; Import Android.os.Bundle; Import Android.view.View; Import Android.widget.Button; Import Android.widget.TextView; Import Android.widget.Toast; public class Mainactivity extends activity { Private String NewName = "Htys.mp3"; The local file path to upload Private String UploadFile = "/data/data/com.xzq/htys.mp3"; Upload to the server at the specified location Private String ActionURL = "jsp" >HTTP://192.168.100.100:8080/UPLOAD/UPLOAD.JSP; Private TextView MTextView1; Private TextView mTextView2; Private Button MButton1; @Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.main); MTextView1 = (TextView) Findviewbyid (R.ID.MYTEXT2); Mtextview1.settext ("filepath:/n" + uploadfile); MTextView2 = (TextView) Findviewbyid (R.ID.MYTEXT3); Mtextview2.settext ("uploadpath:/n" + ActionURL); /* Set Mbutton onclick event handling * * MButton1 = (Button) Findviewbyid (R.id.mybutton); Mbutton1.setonclicklistener (New View.onclicklistener () { public void OnClick (View v) { UploadFile (); } }); } private void UploadFile () { String end = "/r/n"; String hyphens = "--"; String boundary = "* * *"; Try { URL url = new URL (actionurl); HttpURLConnection con = (httpurlconnection) url.openconnection (); /* Allow input, Output, do not use cache */ Con.setdoinput (TRUE); Con.setdooutput (TRUE); Con.setusecaches (FALSE); /* Set the transmitting Method=post * * Con.setrequestmethod ("POST"); * Setrequestproperty * * Con.setrequestproperty ("Connection", "keep-alive"); Con.setrequestproperty ("Charset", "UTF-8"); Con.setrequestproperty ("Content-type", "multipart/form-data;boundary=" + boundary); /* Set DataOutputStream * * DataOutputStream ds = new DataOutputStream (Con.getoutputstream ()); Ds.writebytes (hyphens + boundary + end); Ds.writebytes ("Content-disposition:form-data;") + "name=/" file1/"filename=/" "+ NewName +"/"" + End); Ds.writebytes (end); * * To obtain documents FileInputStream * * FileInputStream fstream = new FileInputStream (uploadfile); /* Set 1024bytes per write/* 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 (hyphens + boundary + hyphens + end); Fstream.close (); Ds.flush (); /* Obtain Response content * * InputStream is = Con.getinputstream (); int ch; StringBuffer B = new StringBuffer (); while (ch = is.read ())!=-1) { B.append ((char) ch); } System.out.println ("Upload success"); Toast.maketext (Mainactivity.this, "Upload success", Toast.length_long) . Show (); Ds.close (); catch (Exception e) { System.out.println ("Upload failure" + e.getmessage ()); Toast.maketext (Mainactivity.this, "Upload failed" + e.getmessage (), Toast.length_long). Show (); } } } |