2011-07-22 27 views
5

hình ảnh thường đòi hỏi tiêu đề HTTP đặc biệt như:Tôi có thể tải lên hình ảnh và văn bản bằng UrlEncodedFormEntity cho nhiều phần không?

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

Tôi đang xây dựng POST của tôi sử dụng:

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

urlEncodedFormEntity phép setContentType, nhưng tôi không thấy làm thế nào tôi có thể kết hợp cả hai hình ảnh và văn bản ??

Trả lời

5
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 { 

} 

trong này thuộc tính vấn đề sẽ mang hình ảnh và myString mang chuỗi ...

Các vấn đề liên quan