Android uploads files to Wamp server

Source: Internet
Author: User

1.php Server (WAMP) section

Set up the unload.php page code as follows

<?phpmove_uploaded_file ($_files["File1" ["Tmp_name"],      "upload/". $_files["File1" ["name"]);      echo "is stored in:". " Upload/". $_files["File1" ["Name"];  

  

Need to configure directory permissions for C:\Windows\Temp, user group has permission to write


Additional IP access settings, c:\wamp\alias\adt.conf in Wamp
Add allow from 192.168.0.102 specified IP

2.android part

Main.java

Package Com.test;import Java.io.bufferedreader;import Java.io.bytearrayoutputstream;import Java.io.dataoutputstream;import Java.io.file;import Java.io.fileinputstream;import Java.io.InputStream;import Java.net.httpurlconnection;import Java.net.url;import Java.util.hashmap;import Java.util.Map;import Android.app.activity;import Android.app.alertdialog;import Android.app.dialog;import Android.content.dialoginterface;import Android.content.intent;import Android.net.uri;import Android.os.Bundle; Import Android.os.handler;import android.os.message;import Android.view.view;import Android.widget.button;import Android.widget.progressbar;import Android.widget.textview;import Android.widget.toast;public class Main extends Activity {/* * variable declaration newName: file name on the server after uploading UploadFile: file path to upload actionurl: the corresponding program path on the server */private String newName = "Pp.jpg";p Rivate string uploadfile = "/data/pp.jpg";p rivate string actionurl = "http://192.168.0.102/tp/upload.php";p rivate TextView mtext1;private TextView Mtext2;privAte Button mbutton;private String msg_result = null;public static final int REFRESH = 0x000001;private static final int FI Le_select_code = 1;p rotected static final int progress_visible = 10;protected static final int progress_invisible = 11;PR Ivate Handler Mhandler = null;private ProgressBar progressBar1 = null; @Overridepublic void OnCreate (Bundle savedinstances Tate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main);p rogressBar1 = (ProgressBar) Findviewbyid ( R.id.progress_horizontal); mText1 = (TextView) Findviewbyid (R.ID.MYTEXT2); Mtext1.settext ("File path: \ n" + uploadfile); MText2 = (TextView) Findviewbyid (R.ID.MYTEXT3) mtext2.settext ("Upload URL: \ n" + actionurl);/* Set Mbutton onclick event handling */ Mbutton = (Button) Findviewbyid (R.id.mybutton); Mbutton.setonclicklistener (new View.onclicklistener () {public void OnClick (View v) {//showfilechooser (); File F = new file (uploadfile), if (!f.exists ()) {ShowDialog (UploadFile + "\ r \ n File not present"), return; New MyThread (). Start ();}); Mhandler = new Handler () {@overridepublic void Handlemessage (Message msg) {if (Msg.what = = REFRESH) {showDialog (msg_result);} else if (msg.what = = progress_visible) {progressbar1.setvisibility (view.visible);} else if (msg.what = = progress_invisible) {progressbar1.setvisibility (view.invisible);} Super.handlemessage (msg);}};} /* Method of uploading files to server */private void UploadFile () {String end = "\ r \ n"; String Twohyphens = "--"; 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);/* Sets the Method=post */con.setrequestmethod ("POST") to be transmitted;/* 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 ());d s.writebytes (twohyphens + boundary + end);d s.writebYtes ("Content-disposition:form-data;") + "name=\" file1\ "filename=\" "+ newName +" \ "" + End);d s.writebytes (end);/* Get file FileInputStream */fileinputstream FStream = new FileInputStream (uploadfile);/* Set each write 1024bytes */int buffersize = 1024;byte[] buffer = new Byte[buffersize]; int length = -1;/* reads data from file to buffer */int sum = 0; Message msg = new Message (); msg.what = Progress_visible;mhandler.sendmessage (msg); while (length = fstream.read (buffer) )! =-1) {sum + = length;/* writes the data to DataOutputStream in */ds.write (buffer, 0, length);} Ds.writebytes (end);d s.writebytes (twohyphens + boundary + twohyphens + end);/* Close Streams */fstream.close ();d S.flush ( );/* Get response content */inputstream is = Con.getinputstream ();  Bytearrayoutputstream BOS = new Bytearrayoutputstream (1000);              Byte[] by = new Byte[1000];int n = 0;while ((n = is.read (by))! =-1) {Bos.write (by, 0, N); } Msg_result = Bos.tostring ();/* Close DataOutputStream */ds.close (); msg = new Message (); msg.what = Progress_invisible;mHandler.sendmessage (msg);} catch (Exception e) {msg_result = E.getmessage ();}} /* Displays dialog's method */private void ShowDialog (String mess) {new Alertdialog.builder (main.this). Settitle ("Message1"). Setmessage (Mess). Setnegativebutton ("OK", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int which) {}}). Show (); public class MyThread extends Thread {public void run () {uploadfile (); Message msg = new Message (); msg.what = Refresh;mhandler.sendmessage (msg);}}}

Main.xml file for layout

<?xml version= "1.0" encoding= "Utf-8"? ><absolutelayout android:id= "@+id/layout1" android:layout_width= " Fill_parent "android:layout_height=" fill_parent "android:background=" @drawable/white "xmlns:android="/HTTP/ Schemas.android.com/apk/res/android "> <textview android:id=" @+id/mytext1 "android:layout_width=" Wrap_ Content "android:layout_height=" wrap_content "android:text=" @string/str_title "android:textsize=" 20sp "Android    : textcolor= "@drawable/black" android:layout_x= "10px" android:layout_y= "12px" > </TextView> <textview Android:id= "@+id/mytext2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:tex Tsize= "16sp" android:textcolor= "@drawable/black" android:layout_x= "10px" android:layout_y= "52px" > </text view> <textview android:id= "@+id/mytext3" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "Android:textsize=" 16SP "ANDROID:TEXTCOlor= "@drawable/black" android:layout_x= "10px" android:layout_y= "102px" > </TextView> <button Android     Oid:id= "@+id/mybutton" android:layout_width= "92px" android:layout_height= "49px" android:text= "@string/str_button" Android:textsize= "15SP" android:layout_x= "90px" android:layout_y= "170px" > </Button> <progre Ssbar android:id= "@+id/progress_horizontal" android:layout_width= "292px" android:layout_height= "49 PX "android:layout_x=" 90px "android:layout_y=" 250px "android:max=" "android:progress=" 0 "android:secondaryprogress=" 0 "android:visibility=" invisible "/></absolutelayout>

  

Androidmanifest.xml need to add permissions

<uses-permission android:name= "Android.permission.INTERNET"/>

Case

Android uploads files to Wamp server

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.