2015-08-16 58 views
1

我有一个android app,它使用来自apache httpcomponents的multipartentity。应用程序使用HTTP请求将文件上传到php脚本。然而,我上传的文件(.ogg)以$ _POST而不是$ _FILES结尾,它看起来像二进制文件,虽然我可能是错的。

我有铬休息扩展,我用它来发布一个完全相同的.ogg文件的多部分/表单数据请求,文件结束在正确的位置($ _FILES)。

Android的代码如下:

// Send the file to the server. 
// Create HTTP objects. 
// Request. 
HttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(ACTION); 

// Response. 
HttpResponse httpResponse; 

httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 

MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
ContentBody cbFile = new FileBody(mediaFile, "audio/ogg"); 
mpEntity.addPart("file", cbFile); 

httpPost.setEntity(mpEntity); 

// Execute. 
httpResponse = httpClient.execute(httpPost); 

// Evaluate the response. 
int statusCode = httpResponse.getStatusLine().getStatusCode(); 

// Make sure everythings okay. 
if (statusCode == 200) { 
    HttpEntity resEntity = httpResponse.getEntity(); 
    if (resEntity != null) { 
      String body = EntityUtils.toString(resEntity); 
      resEntity.consumeContent(); 
      Log.d("APP_STATUS", body); 
    } 
} 

httpClient.getConnectionManager().shutdown(); 

而且非常简单的PHP脚本:使用时

enter image description here

响应:使用应用程序时

<?php 
echo "\$_POST"; 
var_dump($_POST); 
echo "\$_FILES"; 
var_dump($_FILES); 
?> 

响应Chrome REST扩展程序:

enter image description here

如果任何人有任何洞察力,我不能史诗般的,请让我知道。

+0

你可以访问'MultipartEntityBuilder'? – Victory

+0

我相信是这样的,我使用的代码来自类似的情况,除了他们正在上传图像文件。 –

回答

0

尝试使用实体建设者

HttpEntity mpEntity = MultipartEntityBuilder.create() 
    .addBinaryBody(
     "file", cbFile, ContentType.create("audio/ogg"), cbFile.getName()) 
    .build(); 
+0

真棒,你在摇滚,非常感谢! –