Android File Upload

Source: Internet
Author: User

Package com.figo.uploadfile;

Import Java.io.BufferedReader;

Import Java.io.DataOutputStream;

Import Java.io.FileInputStream;

Import Java.io.InputStream;

Import Java.io.InputStreamReader;

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 Uploadfileactivity extends Activity

{

file path to upload, theoretically can transfer any file, actual use as needed to handle

Private String UploadFile = "/sdcard/testimg.jpg";

Private String Srcpath = "/sdcard/testimg.jpg";

The processing page for receiving files on the server, which is replaced by your own

Private String ActionURL = "http://10.100.1.208/receive_file.php";

Private TextView MText1;

Private TextView mText2;

Private Button Mbutton;

@Override

public void OnCreate (Bundle savedinstancestate)

{

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

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 the onclick event handling for Mbutton */

Mbutton = (Button) Findviewbyid (R.id.mybutton);

Mbutton.setonclicklistener (New View.onclicklistener ()

{

@Override

public void OnClick (View v)

{

UploadFile (actionurl);

}

});

}

/* Upload file to Server,uploadurl: Receive file Processing page */

private void UploadFile(String uploadurl)

{

String end = "\ r \ n";

String Twohyphens = "--";

String boundary = "******";

Try

{

URL url = new URL (uploadurl);

HttpURLConnection httpurlconnection = (httpurlconnection) URL

. OpenConnection ();

Set the stream size for each transfer to effectively prevent the phone from crashing due to low memory

This method is used to enable a stream that does not have an internally buffered HTTP request body when the content length is not known beforehand.

Httpurlconnection.setchunkedstreamingmode (* +);//128K

Allow input/output stream

Httpurlconnection.setdoinput (true);

Httpurlconnection.setdooutput (true);

Httpurlconnection.setusecaches (false);

Using the Post method

Httpurlconnection.setrequestmethod ("POST");

Httpurlconnection.setrequestproperty ("Connection", "keep-alive");

Httpurlconnection.setrequestproperty ("Charset", "UTF-8");

Httpurlconnection.setrequestproperty ("Content-type",

"multipart/form-data;boundary=" + boundary);

DataOutputStream dos = new DataOutputStream (

Httpurlconnection.getoutputstream ());

dos.writebytes (twohyphens + boundary + end);

Dos.writebytes ("Content-disposition:form-data; Name=\ "Uploadedfile\"; Filename=\ ""

+ srcpath.substring (Srcpath.lastindexof ("/") + 1)

+ "\""

+ end);

Dos.writebytes (end);

FileInputStream fis = new FileInputStream (Srcpath);

byte[] buffer = new byte[8192]; 8k

int count = 0;

Read file

while ((count = fis.read (buffer))! =-1)

{

Dos.write (buffer, 0, count);

}

Fis.close ();

Dos.writebytes (end);

Dos.writebytes (twohyphens + boundary + twohyphens + end);

Dos.flush ();

InputStream is = Httpurlconnection.getinputstream ();

InputStreamReader ISR = new InputStreamReader (IS, "utf-8");

BufferedReader br = new BufferedReader (ISR);

String result = Br.readline ();

Toast.maketext (this, result, Toast.length_long). Show ();

Dos.close ();

Is.close ();

} catch (Exception e)

{

E.printstacktrace ();

Settitle (E.getmessage ());

}

}

}

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.