2015-08-16 60 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

真棒,你在搖滾,非常感謝! –