Android uploads images to the php Server

Source: Internet
Author: User
: This article mainly introduces how to upload images from android to the php Server. For more information about PHP tutorials, see. Android code

Public class EX08_11 extends Activity
{
/* Variable Declaration
* NewName: name of the uploaded file on the server
* UploadFile: Path of the file to be uploaded
* ActionUrl: the corresponding program path of the server */
// Private String newName = "345444.jpg ";
Private String uploadFile = "/sdcard/345444.jpg ";
Private String acti // **********/upload. 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 processing for mButton */
MButton = (Button) findViewById (R. id. myButton );
MButton. setOnClickListener (new View. OnClickListener ()
{
Public void onClick (View v)
{
UploadFile ();
}
});
}

/* Method of the Server for uploading files */
Private void uploadFile ()
{
// String end = "\ r \ n ";
// String twoHyphens = "--";
String boundary = "*****";
Try
{
URL url = new URL (actionUrl );
HttpURLConnection con = (HttpURLConnection) url. openConnection ();
/* Allow Input and Output without using Cache */
// Con. setReadTimeout (5*1000 );
Con. setDoInput (true );
Con. setDoOutput (true );
Con. setUseCaches (false );
/* Set the transfer method = POST */
Con. setRequestMethod ("POST ");
/* SetRequestProperty */
Con. setRequestProperty ("Connection", "Keep-Alive ");
Con. setRequestProperty ("Charset", "UTF-8 ");
Con. setRequestProperty ("enctype ",
"Multipart/form-data; boundary =" + boundary );
/* Set DataOutputStream */
DataOutputStream ds =
New DataOutputStream (con. getOutputStream ());
/* Ds. writeBytes (twoHyphens + boundary + end );
Ds. writeBytes ("Content-Disposition: form-data;" +
"Name = \" file1 \ "; filename = \" "+
NewName + "\" "+ end );


Ds. writeBytes (end );*/

/* Obtain the FileInputStream of the file */
FileInputStream fStream = new FileInputStream (uploadFile );
/* Set 1024 bytes for each write */
Int buffer size = 1024;
Byte [] buffer = new byte [bufferSize];

Int length =-1;
/* Read data from the file to the buffer */
While (length = fStream. read (buffer ))! =-1)
{
/* Write data to DataOutputStream */
Ds. write (buffer, 0, length );
}
// Ds. writeBytes (end );
// Ds. writeBytes (twoHyphens + boundary + twoHyphens + end );

/* Close streams */
FStream. close ();
Ds. flush ();


/* Get Response content */
InputStream is = con. getInputStream ();
Int ch;
StringBuffer B = new StringBuffer ();
While (ch = is. read ())! =-1)
{
B. append (char) ch );
}
/* Display Response in Dialog */
ShowDialog (B. toString (). trim ());
/* Disable DataOutputStream */
Ds. close ();
}
Catch (Exception e)
{
ShowDialog ("" + e );
}
}

/* Display Dialog's method */
Private void showDialog (String mess)
{
New AlertDialog. Builder (EX08_11.this). setTitle ("Message ")
. SetMessage (mess)
. SetNegativeButton ("OK", new DialogInterface. OnClickListener ()
{
Public void onClick (DialogInterface dialog, int which)
{
}
})
. Show ();
}
}

Php code

$ Data = file_get_contents ('php: // input ');
$ Time = date ("YmdHis ");
$ Rand = rand (0,100 );
$ Filename = $ _ SERVER ['document _ root']. '/image/'.?time.=rand.'.jpg ';
While (file_exists ($ filename ))
{
$ Filename = $ _ SERVER ['document _ root']. '/image/'.20.time.rand(0, 1002.16.'.jpg ';
}

Echo $ filename;
$ Handle = fopen ($ filename, 'w ');
If ($ handle)
{

Fwrite ($ handle, $ data );
Fclose ($ handle );

Echo "success ";
}
?>

The above describes how to upload images from android to the php server, including some content. I hope my friends who are interested in the PHP Tutorial can help me.

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.