如题,请教一个用httpclient通过post数据发送图片和文字的例子(要同时发送)
分不够可以再加

解决方案 »

  1.   

    multipart 文件上传标准 既可以传文字 又可以传文件内容
      

  2.   

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (resultCode == RESULT_OK) {
         EditText et = (EditText)findViewById(R.id.txtMemo);
         try{
         //获取虚幻中的文件路径
         Uri uri = data.getData();
            String[] proj = { MediaStore.Images.Media.DATA };   
                    Cursor actualimagecursor = managedQuery(uri,proj,null,null,null);   
                    int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);   
                    actualimagecursor.moveToFirst();   
                    String img_path = actualimagecursor.getString(actual_image_column_index);
                    
                    //获取选中的文件
                    File file = new File(img_path);                //从基站获取当前地理位置信息
                    TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
              GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
                    
              int cid = 0;
         int lac = 0;
         int mcc = 0;
         int mnc = 0;
             
         if(gcl != null){
         cid = gcl.getCid();
         lac = gcl.getLac();
         mcc = Integer.valueOf(tm.getNetworkOperator().substring(0, 3));
         mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5));
        
         //首先创建HttpPost对象
         HttpPost post= new HttpPost("http://192.168.1.123:8011/SetTrackData.aspx?op=GetImage");
         MultipartEntity entity = new MultipartEntity();
        
         //设置POST参数
         entity.addPart("CID",new StringBody(cid + ""));
         entity.addPart("LC",new StringBody(lac + ""));
         entity.addPart("MMC",new StringBody(mcc + ""));
         entity.addPart("MNC",new StringBody(mnc + ""));
         entity.addPart("IMEI",new StringBody(tm.getDeviceId()));
         entity.addPart("IRM",new StringBody(et.getText() + ""));
         entity.addPart("file",new FileBody(file));
        
         HttpClient client = new DefaultHttpClient();
         HttpResponse response=client.execute(post);
         }
        
         ContentResolver cr = this.getContentResolver(); 
         Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
         ImageView iv = (ImageView)findViewById(R.id.iv01);
         iv.setImageBitmap(bitmap);  
        
         et.setText(img_path);
         }
         catch(Exception e){
         et.setText(e.getMessage());
         }
         }
         super.onActivityResult(requestCode, resultCode, data);
        }
    这是上传代码     private void GetImage()
        {
            HttpFileCollection upFiles = Request.Files;
            string backName = Request.Form["FileName"] ?? "" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
            string path = Server.MapPath(@"~\DataImages\") + backName;
            string result = "恢复数据库失败。";        if (upFiles.Count != 0)
            {
                // 只取第 1 个文件
                HttpPostedFile hpf = upFiles[0];
                if (hpf != null && hpf.ContentLength > 0)
                {
                    // flash 会自动发送文件名到 Request.Form["fileName"]
                    hpf.SaveAs(path);                if (File.Exists(path))
                    {
                        string sqlCmd = "INSERT INTO [ImageData]([ImagePatch],[SendImageIMEI],[SendImageLng],[SendImageLat],[ImageReMark])";
                        sqlCmd += "Values(@ImagePatch,@SendImageIMEI,@SendImageLng,@SendImageLat,@ImageReMark)";                    CellInfo ci = new CellInfo();
                        GeoLocation gl = new GeoLocation();
                        OtherMethod om = new OtherMethod();                    ci.CID = Request.Form["CID"] ?? "";
                        ci.LAC = Request.Form["LC"] ?? "";
                        ci.MCC = Request.Form["MMC"] ?? "460";
                        ci.MNC = Request.Form["MNC"] ?? "0";                    string jsonData = gl.GenerateRequestJson(ci);
                        string resultData = om.httpSendData("http://www.google.com/loc/json", jsonData);
                        int s = resultData.IndexOf(':');
                        int e = resultData.LastIndexOf(',');
                        LocationObject lo = gl.GetObjData(resultData.Substring(s + 1, e - s));                    if (!string.IsNullOrEmpty(lo.longitude))
                        {
                            string[,] parms = {
                                       {"ImagePatch",path},
                                       {"SendImageIMEI",Request.Form["IMEI"] ?? ""},
                                       {"SendImageLng",lo.longitude},
                                       {"SendImageLat",lo.latitude},
                                       {"ImageReMark",Server.HtmlDecode(Request.Form["IRM"] ?? "")}
                                  };                        
                        DBOperation dbo = new DBOperation();
                        dbo.RunSql(sqlCmd, parms);
                        }
                    }
                }
            }
        }
    这是接收端代码,就是死活能接收参数不能接收文件