【问题标题】:Can I upload Images AND text using UrlEncodedFormEntity for multi part?我可以使用 UrlEncodedFormEntity 为多部分上传图像和文本吗?
【发布时间】:2011-07-22 23:12:11
【问题描述】:

图像通常需要特殊的 HTTP 标头,例如:

Content-disposition: attachment; filename="file2.jpeg"
Content-type: image/jpeg
Content-Transfer-Encoding: binary

我正在使用以下方法构建我的 POST:

List<NameValuePair> formparams = new ArrayList<NameValuePair>();
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams);

urlEncodedFormEntity 允许 setContentType,但我看不到如何混合图像和文本??

【问题讨论】:

    标签: java android image http multipart


    【解决方案1】:
    try {
       File file = new File(Environment.getExternalStorageDirectory(),"FMS_photo.jpg");
    
       HttpClient client = new DefaultHttpClient();  
       HttpPost post = new HttpPost("http://homepage.com/path");  
       FileBody bin = new FileBody(file);  
    
    
       Charset chars = Charset.forName("UTF-8");
    
       MultipartEntity reqEntity = new MultipartEntity();  
       reqEntity.addPart("problem[photos_attributes][0][image]", bin);  
       reqEntity.addPart("myString", new StringBody("17", chars));
    
       post.setEntity(reqEntity); 
       HttpResponse response = client.execute(post);  
    
       HttpEntity resEntity = response.getEntity();  
       if (resEntity != null) {    
         resEntity.consumeContent();  
      }
    
       return true;
    
      } catch (Exception ex) {
    
        globalStatus = UPLOAD_ERROR;
        serverResponse = "";
        return false;
      } finally {
    
     }
    

    在此问题属性将携带图像,myString 携带字符串...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多