【问题标题】:android multipart image upload with json object带有json对象的android多部分图像上传
【发布时间】:2014-05-06 05:48:57
【问题描述】:

我想上传图片到服务器。

这是代码,

  try {

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.yigit);
        Charset chars = Charset.forName("UTF-8"); // Setting up the encoding

        MultipartEntity reqEntity = new MultipartEntity();
        StringBody jsonBody = new StringBody(getNewDemandRequestParams(), "application/json",null);
        FormBodyPart jsonBodyPart = new FormBodyPart("data", jsonBody);
        reqEntity.addPart(jsonBodyPart);

      if (getMainActivity().getImagesSavedData(0).size() > 0) {
            for (int i = 0; i < getMainActivity().getImagesSavedData(0).size(); i++) {
  File _file = new File(getMainActivity().getImagesSavedData(0).get(i).getFilePath());               
                FileBody _fileBody = new FileBody(_file, "image/jpg", "UTF-8");
 FormBodyPart fileBodyPart = new FormBodyPart(getMainActivity().getImagesSavedData(0).get(i).getImageName().replace(".jpg", ""), _fileBody);

                reqEntity.addPart(fileBodyPart);

                reqEntity.addPart(getMainActivity().getImagesSavedData(0).get(i).getImageName().replace(".jpg",""), _fileBody);



          }
        }

        post.setEntity(reqEntity);
        String result = EntityUtils.toString(reqEntity);
        Log.e("rsul", result);
        HttpResponse response = client.execute(post);
        resEntity = response.getEntity();

        final String response_str = EntityUtils.toString(resEntity);

}

但问题是 jsonBodyPart 包含斜线。

请求正文如下:

{"data"=>"{\"action\":\"YENITALEP\",\"app\":{\"version\":\"verisyon\"},\"data\":{ \"invoices\":[{\"imageName\":\"1395914025134\",\"note\":\"\",\"type\":\"FATURA\",\"typeNo\":\ "0\"}],\"note\":\"\",\"notification\":[{\"type\":\"BeniAray?n\",\"typeNo\":\"0\ "}]},\"设备\":{\"硬件型号\":\"m7\",\"型号\":\"HTC One\",\"systemVersion\":\"4.4.2\",\"uid\":\"00000000-7f39-faab-b500-7f280e9b4fed\"},\"timestamp\":\"Date(1391073711000 +0200)\"}", “1395914025134”=>#, @original_filename="1395914025134.jpg", @content_type="image/jpg; charset=UTF-8", @headers="Content-Disposition: form-data; 名称=\"1395914025134\"; 文件名=\"1395914025134.jpg\"\r\n内容类型:image/jpg; charset=UTF-8\r\n内容传输编码:二进制\r\n">}

如何使用 multipart 发布复杂的 json 对象和图像?感谢帮助

【问题讨论】:

    标签: android json image multipartform-data slash


    【解决方案1】:

    检查一下这个代码,我使用这个代码将图像上传到服务器

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(urls[0]);
    MultipartEntity multipartContent = new MultipartEntity();
    for(int i=0;i<allimagespath.size();i++){
    Bitmap bm = ShrinkBitmap(allimagespath.get(i), 140, 140);
    String format = allimagespath.get(i).substring((allimagespath.get(i).lastIndexOf(".")+1) ,  allimagespath.get(i).length());
    Bitmap bit=Bitmap.createScaledBitmap(bm, 140, 140, true);
    ByteArrayOutputStream blob = new ByteArrayOutputStream();
     if(format.equalsIgnoreCase("png")){
        bit.compress(CompressFormat.PNG, 100 , blob);
     }else{
        bit.compress(CompressFormat.JPEG, 100 , blob);
     }
    bitmapdata = blob.toByteArray();
    ByteArrayBody thumbbmp = new ByteArrayBody(bitmapdata, "thumb."+format);
    FileBody bin2 = new FileBody(new File(allimagespath.get(i)));               
    multipartContent.addPart("uploaded_file["+i+"]", bin2);
    multipartContent.addPart("uploaded_thumb["+i+"]", thumbbmp);
    }
    multipartContent.addPart("count", new StringBody(""+allimagespath.size()));
    postRequest.setEntity(multipartContent);
    HttpResponse response = httpClient.execute(postRequest);
    HttpEntity entity = response.getEntity();
    is = entity.getContent(); 
    

    【讨论】:

      猜你喜欢
      • 2013-10-12
      • 2016-04-10
      • 2017-01-10
      • 2013-07-19
      • 2011-11-27
      • 2019-05-01
      • 1970-01-01
      • 2015-11-26
      • 2016-07-30
      相关资源
      最近更新 更多