为什么我不能用java和php编码和解码base64?

时间:2018-01-16 22:47:05

标签: java php android base64

我使用php对服务器上的图像进行编码,并通过get请求将编码后的字符串发送到android,然后用java解码,但输出错误的base64。

所以我决定检查在线检查器上的base64字符串,但是没有出现图像是php编码的问题?

这是编码图像字符串:

aW1hZ2VzL21haW4vd3d3L25ldCAtIENvcHkucG5n

PHP ::

<?php


require_once("config.php");

if(isset($_GET["m"])) {

    $dirname = "images/main/";
    $arr = array();

    $conn = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);

    if(!$conn) {
        echo "Error connecting to database";
        exit();
    }
    if($stmt = $conn->prepare("SELECT name_ FROM projects")) {
        $stmt->execute();
        $stmt->bind_result($n);
        //$stmt->store_result();
        $result = $stmt->get_result();
        if($result->num_rows == 0) {
            echo "No Projects";
            $stmt->close();
            $conn->close();
            exit();
        }else {
            while ($row = $result->fetch_assoc()) {
                $dirname = $dirname . $row["name_"] . "/";
                $images = glob($dirname . "*.*", GLOB_BRACE);
                foreach($images as $key => $image) {
                    $image = base64_encode($image);
                    //array_push($arr, $image);
                    $dirname = "images/main/";
                    echo $image;
                    echo "/n";
                    $image = "";
                }
            }
            //echo "hi";//json_encode($arr);
        }
    }

    $stmt->close();
    $conn->close();
    exit();

}

?>

机器人::

 @Override
    protected String doInBackground(String[] params) {
        String add = "http://10.0.2.2/wael/getimages.php?m=all";
        byte[] image = null;
        Bitmap real = null;
        String parsedString = "";
        BufferedReader bufferedReader = null;
        InputStream is = null;
        StringBuilder sb = null;
        HttpURLConnection httpConn = null;
        URLConnection conn = null;
        try {
            URL url = new URL(add);
            conn = url.openConnection();
            httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            is = httpConn.getInputStream();
            sb = new StringBuilder();
            String line;
            bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            bitmaps = new ArrayList<>();
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line).append("\\n");

                //image = Base64.decode(line, Base64.NO_PADDING);
                //real = BitmapFactory.decodeByteArray(image, 0, image.length);
                //bitmaps.add(real);
                //image = null;
                //real = null;
            }
            String[] lines = sb.toString().split("\\n");
            for(String s: lines){
               image = Base64.decode(s, Base64.URL_SAFE);
                real = BitmapFactory.decodeByteArray(image, 0, image.length);
                bitmaps.add(real);
                image = null;
                real = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (httpConn != null) {
                    httpConn.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "done";
    }

提示::

使用java解码的所有类型的模式都不起作用。对于exmaple URL_SAFEDEFAULT等不能正常工作所有输出相同的坏base64

1 个答案:

答案 0 :(得分:1)

你不是base64编码图像,你是base64编码图像的路径。

如果有人说'#34;给我发一张帝国大厦的照片&#34;然后你给他们写了一封信,上面写着#34;帝国大厦的照片&#34; 34 ;.

例如,您在评论中说过

  

以下仍未输出图像aW1hZ2VzL21haW4vd3d3L25ldCAtIENvcHkucG5n

但如果我这样做:

&#13;
&#13;
console.log(atob("aW1hZ2VzL21haW4vd3d3L25ldCAtIENvcHkucG5n"))
&#13;
&#13;
&#13;

当你点击跑步时,你会看到

  

images / main / www / net - Copy.png

显然,不是预期的结果。

在PHP中,你会做这样的事情:

$image = file_get_contents($filename);
$b64_image_raw = base64_encode($im);      
$mime = mime_content_type($filetype)
$b64_image = "data:" . $mime . ";base64," . $b56_image_raw;

会给你类似的东西:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAdCAMAAABhTZc9AAAAP1BMVEU7V507V50AAAA7V507V507V53///9GYaP7/P33+Pvm6vPa4O2aqM3s7/bf5O+8xd6uutigrdBfd7BNZ6c+Wp9WPQrIAAAABXRSTlP0cwDze/4T5ZQAAABkSURBVCjP3c45DoAwDETRcUiclZ37n5UoFUUyEhIVv32yNTAyuX6TGIgbJwDRiqy36kuOGlNfj6StrvpZie7KdKmw+dGqUPUcbm5PP1d9FC6mmd6uVAtf9VcFUcAStTCCgULMDXttET4Wr4wGAAAAAElFTkSuQmCC

&#13;
&#13;
var image = document.createElement("img")
image.src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAdCAMAAABhTZc9AAAAP1BMVEU7V507V50AAAA7V507V507V53///9GYaP7/P33+Pvm6vPa4O2aqM3s7/bf5O+8xd6uutigrdBfd7BNZ6c+Wp9WPQrIAAAABXRSTlP0cwDze/4T5ZQAAABkSURBVCjP3c45DoAwDETRcUiclZ37n5UoFUUyEhIVv32yNTAyuX6TGIgbJwDRiqy36kuOGlNfj6StrvpZie7KdKmw+dGqUPUcbm5PP1d9FC6mmd6uVAtf9VcFUcAStTCCgULMDXttET4Wr4wGAAAAAElFTkSuQmCC";
document.getElementById("image").appendChild(image)
&#13;
<div id="image"></div>
&#13;
&#13;
&#13;