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