2011-02-23 186 views

回答

2

我建議創建一個AsyncTask來處理您的上傳,然後使用Apache Libs來爲您的服務器執行POST。我現在有一個系統將圖像發佈到Linux服務器,然後PHP接受POST並保存圖像數據。嘗試這樣的事情,如果我沒有記錯的話,你需要從Apache獲得commons-io jar和httpmime jar。

import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.mime.MultipartEntity; 
import org.apache.http.entity.mime.content.InputStreamBody; 
import org.apache.http.entity.mime.content.StringBody; 
import org.apache.http.impl.client.DefaultHttpClient; 

HttpClient httpClient = new DefaultHttpClient(); 
HttpPost postRequest = new HttpPost("http://www.mywebserver.com/upload"); 

MultipartEntity multipart = new MultipartEntity(); 
multipart.addPart("photo",bitmapdata); 

postRequest.setEntity(multipart); 
HttpResponse response = httpClient.execute(postRequest); 

InputStream content = response.getEntity().getContent(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
StringBuilder serverMsg = new StringBuilder(); 
String line = ""; 
while((line = reader.readLine()) != null){ serverMsg.append(line + "\n"); } 
content.close(); 

然後在PHP中,檢查POST並使用任何庫或代碼保存圖像。我相信你可以輕鬆地調整此代碼發送視頻數據,而不是照片。

+0

你可以給一些php代碼,我使用這些方法,有時有時會失敗 – pengwang 2011-11-20 09:28:34