我现在正在尝试将图片上传到我的服务器,但却迷失了方向。我能够在 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>';
}
文件已被访问,但仍保持空白。这里我是否遗漏了什么?请帮忙,我尝试在谷歌上搜索,但是得到的结果都没有帮到我太多。