2011-02-18 27 views
1

我想從我的應用程序發送一個JPG到一個PHP監聽器,它看起來像一個ByteArray是正確的方式來處理這個。 但是,當我請求當前已到達服務器時,它具有fileName參數(它是單獨的),但不包含ByteArray參數。在ByteArray的情況下,它缺少字段的鍵和值。如何從Android向PHP發送ByteArray?

在調試器中,我可以看到數據在請求中......也許我沒有正確收集它在PHP端?

這裏是我的代碼的相關部分...所有幫助非常感謝(以及建議以一些替代,更有效的方式做到這一點)。

ANDROID:

try {   
     Part[] parts = new Part[2]; 


     ByteArrayPartSource ps = new ByteArrayPartSource(printFileName, bitmapdata); 
     parts[0] = new FilePart("fileData", ps); 

     parts[1] = new StringPart("fileName" ,printFileName); 

     filePost.setRequestEntity(new MultipartRequestEntity(parts, 
       filePost.getParams())); 

    } catch (Exception e) { 
     Log.d("MY_DEBUG", e.toString()); 
    } 

    try { 
     int statusCode1 = client.executeMethod(filePost); 

PHP:

$targetPath = "./" . $_REQUEST['fileName'] ; 


$newFilePath = $targetPath; 

$fh = fopen($newFilePath, 'wb');  
    //tried both of these, neither seems to work... 
    fwrite($fh, $GLOBALS["HTTP_RAW_POST_DATA"]) or die("unable to write data"); 
      //and 
    fwrite($fh, file_get_contents('php://input')); 
    fclose($fh); 

    echo "success!"; 
+0

嗯......我不知道正是我一直在上面(或許真的在PHP端)發佈的代碼之外做錯了,但這實際上完美地工作。我想刪除這個問題。 – 2011-02-23 00:17:27

回答

1

檢查這裏的鏈接我已經發布了完整的代碼將圖像上傳到PHP服務器

Android httpclient file upload data corruption and timeout issues

http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html

這是一個檢查和測試的事情 希望它能幫助:)

+0

謝謝,我想我應該只使用我的byteStream並稱它爲一天。我從來沒有遇到過從Android上傳文件的問題。我只是決定使用一個ByteArray,認爲它會完全直截了當,但是後來花了4個小時從我的頭上撕下了頭髮,爲什麼它不起作用。 – 2011-02-18 10:03:04