Android Code
public class Ex08_11 extends Activity
{
/* Variable Declaration
* NewName: The file name on the server after uploading
* UploadFile: File path to upload
* ActionURL: Server-Corresponding program path */
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 Mbutton's onclick event handling */
Mbutton = (Button) Findviewbyid (R.id.mybutton);
Mbutton.setonclicklistener (New View.onclicklistener ()
{
public void OnClick (View v)
{
UploadFile ();
}
});
}
/* Upload file to Blow server's method */
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.setreadtimeout (5 * 1000);
Con.setdoinput (TRUE);
Con.setdooutput (TRUE);
Con.setusecaches (FALSE);
/* Set the transmitted 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); */
/* Get the file 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 (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);
}
/* Show Response on Dialog */
ShowDialog (b.tostring (). Trim ());
/* Close DataOutputStream */
Ds.close ();
}
catch (Exception e)
{
ShowDialog ("+e");
}
}
/* Show 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/'. $time. Rand (0,100). JPG ';
}
echo $filename;
$handle = fopen ($filename, ' w ');
if ($handle)
{
Fwrite ($handle, $data);
Fclose ($handle);
echo "Success";
}
?>
The above describes the Android upload image to the PHP server, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.