doumuyu0837 2015-05-24 13:53
浏览 709
已采纳

尝试上传图像时,base64_decode返回null

I am trying to upload an image to server but base64_decode always returns null i read a lot of different cases from SO but nothing works. here is my java code before i make my http request:

      ByteArrayOutputStream stream = new ByteArrayOutputStream();
            // Must compress the Image to reduce image size to make upload easy
            bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream); 
            byte[] byte_arr = stream.toByteArray();
            // Encode Image to String
            encodedString = Base64.encodeToString(byte_arr,Base64.NO_WRAP);
            params.add(new BasicNameValuePair(TAG_IMAGE, encodedString));
           ...and the http request

php code:

                     $base=$_REQUEST['image'];
                          //$response["base"] = $base;
                       $binary=base64_decode($base,TRUE);
                          $response["binary"] = $binary;
                         header('Content-Type: bitmap; charset=utf-8'); 
                         $imagepath='the path of the image';
                            $file = fopen($imagepath, 'w');
                            fwrite($file, $binary);
                            fclose($file);

i also tried $file = fopen($imagepath, 'wb'); still $binary is always return with null; somebody can please help me understand what is the problem?

also i checked $base and it looks ok, i also tried to remove or \\ but nothing helps

  • 写回答

1条回答 默认 最新

  • dqvtm82066 2015-05-31 11:00
    关注

    so i figure it out: 1. the path that you need to save the image must be relative to the web service (php code in my case) in the server which also mean you have to save the image at the same server. 2. file_put_contents is a lot easyier to use and replace the fopen, fwrite and fclose together and save you trubble. i hope it will help you guys

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集