Development of Android mobile phone file upload function implementation

Source: Internet
Author: User
Tags file upload getmessage stringbuffer

File upload in B/S application is a very common function, then under the Android platform can be implemented like b/S file upload function? The answer is yes. Here is an example of a simulated Web site program upload file. Here only the Android part of the code, the server code is not posted, there is a need to complete the function code to upload the friend can contact me yo ...

First create a new Android project, create a new master start activity:

The code is as follows Copy Code

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 ();

}

}

}

Finally, don't forget to set permissions to access the Internet in Androidmanifest.xml:

The code is as follows Copy Code

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

The principle of uploading files on Android is basically consistent with uploading files on the web, and the code is simple enough to explain it.

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.