由于项目需要,做了一个验证程序,将android手机上的图片或文件通过POST方式提交到Linux服务器,服务端为PHP.          


PHP 服务器源码   


<?         
    function upload_image()
   { 
    $db =$this ->loadDB();
    $db->Execute("SET NAMES UTF8"); 
  
    if($_FILES)
    {
     foreach($_FILES as $v){
    
    $extendname = explode( ".",$v[name]);  
    $file_name=$v[name];
    $code=$extendname[0];  
    $exten=$db->GetOne("...");
    //文件保存目录
    $temp_dir="order_images".date("/Y/m/d/",time()).$exten;
    //创建文件
    $this->mkdirs($temp_dir);
    //保存图片
    if(copy($v[tmp_name], $temp_dir."/".$v[name]))
    {
     //..........
    }else 
    {
     echo "f";
    } 
     }
    }
   }

?>


 Android 客户端source


//=============================
package irdc.ex08_11;

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.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/* import相关class */

public class EX08_11 extends Activity
{
  /*
   * 变量声明 filename:上传后在服务器上的文件名称 uploadFile:要上传的文件路径 actionUrl:服务器上对应的程序路径
   */
 //http://192.168.2.210/userweb_express/index.php?module=api&action=
  private String uploadFile = "/data/42885214552.jpg";
  private String srcPath = "/data/42885214552.jpg";
  private String actionUrl = "http://192.168.2.210/userweb_express/index.php?module=api&action=upload_image";
  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("文件路径:\n" + uploadFile);
    mText2 = (TextView) findViewById(R.id.myText3);
    mText2.setText("上传网址:\n" + actionUrl);
    /* 设置mButton的onClick事件处理 */
    mButton = (Button) findViewById(R.id.myButton);
    mButton.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View v)
      {
         uploadFile();
        //String url = " http://just2009.8866.org:9238/userweb_express/justapi/index.php?module=api&action=user_login&username=502&password=502";
       // request(url);
      }
    });
  }

  /* 上传文件至Server的方法 */
  private void uploadFile()
  {

    String uploadUrl =  "http://192.168.2.210/userweb_express/index.php?module=api&action=upload_image";
    String end = "\r\n";
    String twoHyphens = "--";
    String boundary = "******";
    try
    {
      URL url = new URL(uploadUrl);
      HttpURLConnection httpURLConnection = (HttpURLConnection) url
          .openConnection();
      httpURLConnection.setDoInput(true);
      httpURLConnection.setDoOutput(true);
      httpURLConnection.setUseCaches(false);
      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=\"file\"; filename=\""
              + srcPath.substring(srcPath.lastIndexOf("/") + 1)
              + "\"" + end);
      dos.writeBytes(end);

      FileInputStream fis = new FileInputStream(srcPath);
      byte[] buffer = new byte[8192]; // 8k
      int count = 0;
      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());
    }

  }
  
  public static void request(String url) throws Exception {
    URL u = new URL(url);
    InputStream in = u.openStream();
    BufferedReader reader =  new BufferedReader(new InputStreamReader(in));
    String line = null;
    StringBuffer sb = new StringBuffer();
    while((line = reader.readLine()) != null){
      sb.append(line).append("\r\n");
    }
    in.close();
    reader.close();
    System.out.println(sb.toString());
  }

}