0

嗨,我正在做一个课程作业,我需要将数据发送到带有 PHP 后端的远程 MySQL 服务器。目前我在发送和接收图像时遇到了一些问题。

我目前正在做的是将图像编码为 base64 并通过 http post 将其发送到我的服务器。我已经检查了正在发送的数据,一切都很好。但是,我的 PHP 方面并没有正确处理它。

这就是我正在做的...

这是我用于将位图转换为字符串的方法...

  ByteArrayOutputStream baos=new  ByteArrayOutputStream();
  image.compress(Bitmap.CompressFormat.JPEG, 90, baos);
    byte [] b=baos.toByteArray();
    String imageString=Base64.encodeToString(b, Base64.DEFAULT);

然后我在 http 帖子中发送这个 imageString,它被下面的 PHP 脚本接收

$uploadDIR = '../data/';
if ($_REQUEST['picture']!=null){

    $image = base64_decode($_REQUEST['picture']);
    $file = $uploadDIR . uniqid().'.jpeg';
file_put_contents($file, $image);
    $sql = "
                                            INSERT INTO comments
                                            (image)
                                            VALUES (
                                                    '".$file."'
                                                    )";


}
executeSQLQuery($sql);

在尝试了几次之后,甚至文件都没有存储在该位置。

在此先感谢您的帮助:)

4

1 回答 1

0

java中的代码假设图像已经转换为位图:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmIcone.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
namevaluepair.add(new BasicNameValuePair("image", image_str));

在服务器中:

$base= $_REQUEST['image'];
$buffer = base64_decode($base);

$buffer = mysql_real_escape_string($buffer);
于 2014-11-11T08:06:19.783 回答