我現在試圖將圖像上傳到我的服務器。我可以拍攝照片並在Android設備上獲取我的位置。我有如下代碼將文件上傳到服務器:將圖像上傳到服務器PHP Android
public Boolean postFunction(File image) {
String tag = "postFunction";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(UPLOAD_URL);
try {
MultipartEntity entity = new MultipartEntity();
entity.addPart("type", new StringBody("photo"));
entity.addPart("data", new FileBody(image));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
Log.i(tag, "picture was uploaded " + response.toString());
return true;
} catch (ClientProtocolException e) {
Log.e(tag, "Client: " + e.toString());
return false;
} catch (IOException e) {
Log.e(tag, "IO: " + e.toString());
return false;
}
}
我的圖像文件通過這個功能,它的上傳。然而,錯誤在於我相信服務器端。
這是我的PHP代碼:
public function upload() {
//$type = $this->input->post('type');
//$data = $this->input->post('data');
$base = $_REQUEST['data'];
echo $base;
// base64 encoded utf-8 string
$binary = base64_decode($base);
// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
// print($binary);
//$theFile = base64_decode($image_data);
$file = fopen('./test.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=test.jpg>';
}
的文件被感動,但仍保持空白。我在這裏錯過了什麼嗎?請幫忙,我嘗試了谷歌搜索,但想出了不同的結果,這並沒有多大幫助。