Android将Base64字符串发布到PHP

时间:2012-03-29 07:35:06

标签: php android post base64

--- ---解决方案--- ---
问题发生在我们的服务器上。如果我们将www放在我们的域名前面,它只能处理邮件请求。这就是导致问题的原因。我将第一个答案设置为答案,因为一旦我对URL进行排序,它就起作用了。


原始问题

我的PHP脚本中有一个POST变量,总是空白的 我试图改变变量的名称,变量的内容等。

问题必须存在于java代码中,因为当我在php中var_dump()请求时,它是null。

这是我的情景:

我让用户拍摄照片,照片会保存到SD卡中,然后我会获得图片的路径,并最终将其转换为Base64字符串。然后我想将这个base64字符串发布到一个php脚本,该脚本将它从Base64转换回来并将其作为图像写在我服务器的硬盘上,如下所示:

File f = new File(savedImagePath);

Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());          
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream); 
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://myURL/uploadImage.php");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();

pairs.add(new BasicNameValuePair("image", image_str));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);

在服务器端,这就是我的PHP脚本:

<?php
$base = $_POST["image"];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'w');
fwrite($file, $binary);
fclose($file);

echo "script done executing";
?>

这是我试图发布的Base64字符串的示例:

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABsSFBcUERsXFhceHBsgKEIrKCUlKFE6PTBCYFVlZF9VXVtqeJmBanGQc1tdhbWGkJ6jq62rZ4C8ybqmx5moq6T/2wBDARweHigjKE4rK06kbl1upKSkpKSkpKSkpKSkpKSkpKSkpKSkpKSkpKSkpKSkpKSkpKSkpKSkpKSkpKSkpKSkpKT/wAARCAogBsADASIAAhEBAxEB/8QAHwAAAQUBAQ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FHWgA96B0pPalPXHFFhBRnik+lLQOwZ5o6UdKM/jQAoo70maO1AhT1pMcUg5pc0D0F70daT3paBAKKM0UDYDrSj+dJSigVw70d6QUtAw9zzRSZpc5+tMVwNL7UlH40gCgdaUcUlMApRRR2oAPeiijPFAIBQOtFFAbCikozRQADmlpBS0DAUUnfNL3oEFAo7YoxTAXvSdqO+BzR9KAClHWkooBB0paM0UgFpBSdTQPrmgQtHWjPOaO9MBc0Ug60A0guFLRSUDQUUfSimAtJ3oo70gFzRSUZoAWkozR+lMOgtFJR1oAXNHSkFFAC0dqQdKM0gFopB15oPWmAtA60maO1AIWj8KQUUBYX9aKQGigA9qKM0d6QCig0lFAgo60DpRQMO1ITRmk7UCEprdKcaQ0DGGoyKeaY3ftSAYSKhbpUp7d6jbmkxoiPFN7U5vu9aYeKQx38OR0pU9qb29q

5 个答案:

答案 0 :(得分:3)

这是图片的代码:

ByteArrayOutputStream bao = new ByteArrayOutputStream();                

bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));

这是用于将图像发送到服务器的HTTP代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://servername.com/uploadimage.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));        
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

如果你发现任何困难,请问我或支票:

在PHP头文件更改中:

header('Content-Type: image/jpg; charset=utf-8');
$base=$_REQUEST['image'];

答案 1 :(得分:1)

您是否尝试过在PHP中阅读原始帖子数据?像

这样的东西

$data = $xml = file_get_contents('php://input');

请查看此链接以获取更多信息

http://www.codediesel.com/php/reading-raw-post-data-in-php/

答案 2 :(得分:1)

只需下载Base64.java http://iharder.sourceforge.net/current/java/base64/&amp;把那个文件放在你的项目中那么

    just replace the 

String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT); 

这个用

String image_str = Base64.encodeBytes(byte_arr);

替换此post.setEntity(new UrlEncodedFormEntity(pairs));  与

  post.setEntity(new UrlEncodedFormEntity(pairs,HTTP.UTF_8));

同时检查导入Base64时,它会从您的包中导入文件&amp;然后发布到服务器它将发布。

在您的PHP代码中只需更改

改变这个    $ base = $ _POST [“image”];   到
$ base = $ _REQUEST ['image'];

答案 3 :(得分:1)

尝试按以下方式发送数据:

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 60000);  //1 min
HttpConnectionParams.setSoTimeout(httpParams, 60000);          //1 min

HttpClient client = new DefaultHttpClient(httpParams);
HttpPost post = new HttpPost("http://myURL/uploadImage.php");

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("image", image_str));

post.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));   // as UTF-8
HttpResponse response = client.execute(post);

答案 4 :(得分:1)

你的HTTP响应代码是200吗? 如果是这样,请尝试设置这些标题字段:

“接受”:“text / html”

“接受语言”:“en”

“Content-Length”:yourString.length

“Content-Type”:“application / x-www-form-urlencoded”