Java Post请求不发送文件

时间:2013-10-27 17:06:11

标签: java android http-post

我正在尝试发送包含文件的帖子请求。出于某种原因,我的文件没有被发送。关于为什么会发生这种情况的任何想法?

**  public String postReceipt(String token, String FilePath) throws ClientProtocolException, IOException { 
    Log.i("postData receipt", FilePath);
    File image = new File(FilePath);
    HttpPost httppost = new HttpPost(url);

    Log.i("image name", image.getName());

    MultipartEntity multipartEntity = new MultipartEntity();  
    multipartEntity.addPart("access_token", new StringBody(token));
    multipartEntity.addPart("receipt[image]", new FileBody(image));
    // multipartEntity.addPart("image", new FileBody(image)); -> 500

    multipartEntity.addPart("store[name]", new StringBody("Some Store"));        
    multipartEntity.addPart("store[street]", new StringBody("Backstr. 29")); 
    multipartEntity.addPart("store[zip]", new StringBody("12047"));        
    multipartEntity.addPart("store[city]", new StringBody("Hamburg")); 
    multipartEntity.addPart("store[country]", new StringBody("DE")); 

    httppost.setEntity(multipartEntity);
    HttpClient httpclient = new DefaultHttpClient(httpParameters);

    HttpResponse response = httpclient.execute(httppost);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    response.getEntity().writeTo(out);
    String responseString = out.toString();
    Log.i("postData receipt", response.getStatusLine().toString());
    Log.i("postData receipt", responseString);
    return "DONE";
}

* $ data = array     (         'access_token'=> '248628ecd3342828e6',         'receipt [image]'=> '@d:\ a67ff68fb34113ac300af9dde46b8aec__2.jpg'     );

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_VERBOSE, true); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
$result = curl_exec($curl); 
curl_close($curl);  
print_r($result); ***

**

0 个答案:

没有答案