当我在文件中写入时,谁在Base64编码图像中添加"\n"?Java的

Joa*_*llà 16 java base64 encoding android image

我在做什么

我需要通过HTTPS请求发送一个JsonArray,其中包含Base64编码字符串中的一些数据和图像.如果数据存储在内存中,这很有效.

现在,我需要避免加载内存中的所有数据,我正在Android设备中创建一个临时文件,其中包含我需要发送的所有数据.为了创建文件,我在他里面写了很多JsonObjects.一些JsonObjects有一个表示图像的字段.当我检测到一个时,我得到了图像路径,并使用Base64作为字符串对其进行编码.

更新:

首先,我将文件理论化,然后获得bufferedWriter

            File f = new File(privateSincronizePath+File.separator+"upload_"+timefile+".json");
            f.createNewFile();
            FileWriter fw = new FileWriter(f.getAbsoluteFile(), true);
            BufferedWriter bw = new BufferedWriter(fw);
Run Code Online (Sandbox Code Playgroud)

以下是存在时创建图像的代码:

            JSONObject jobInf = new JSONObject();
            jobInf.put("xx", "INSERT");
            jobInf.put("xx", c.getString(c.getColumnIndex("xx")));
            jobInf.put("xx", ""+c.getInt(c.getColumnIndex("xx")));
            jobInf.put("xx", c.getString(c.getColumnIndex("xx")));

            JSONObject jsonObject = new JSONObject(docu.filtre(dades, docu.getXx()));
            Iterator<?> keys = jsonObject.keys();
            boolean updated = false;
            while(keys.hasNext() && !updated){
                String key = (String)keys.next();
                if (key != null && key.equals("path") && key.length() > 0){
                    jsonObject.put(key, getBase64EncodeFromImage(jsonObject.getString(key)));
                    updated = true;
                }
            }

            jobInf.put("object", jsonObject);

            escriure(bw, ","+jobInf.toString());
Run Code Online (Sandbox Code Playgroud)

方法escriure():

更新:每次完成JsonObject的创建时都会调用此方法.仅将JsonObject作为String附加到文件中.

    private void escriure(BufferedWriter bw, String s) throws IOException {
        uploadLength += s.length();
        bw.append(s);
    }
Run Code Online (Sandbox Code Playgroud)

最后,当创建文件时,我正在读取它,并使用OutputStream,我将数据作为Post参数发送到服务器:

        this.Https_url = new URL(url+"/sync.php?");
        HttpsURLConnection con = (HttpsURLConnection) Https_url.openConnection();
        con.setReadTimeout(60000);
        con.setConnectTimeout(60000);
        con.setRequestMethod("POST");
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setFixedLengthStreamingMode(uploadLength);

        OutputStream os = con.getOutputStream();
        InputStream inp = new FileInputStream(new File(privateSincronizePath+File.separator+"upload_"+timefile+".json"));
        byte[] buffer = new byte[1024];

        int len;
        while ((len = inp.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        inp.close();
        os.close();         

        // Establim la connexió:
        con.connect();
Run Code Online (Sandbox Code Playgroud)

问题是什么

问题很简单.当我在服务器中打开图像时,文件已损坏,并且不显示图像.

我注意到了什么

如果我捕获图像Base64字符串编码,在写入文件之前,并将其上传到服务器中,图像就可以了!然后,在写入文件后,图像似乎已损坏.

经过调查,我注意到Base64编码的字符串在写入文件后,每x个字符都有很多"\n".如果我删除所有这些"\n"或分隔线,服务器可以正确打开图像.

问题

谁在推出这个分界线?如何将Base64编码为字符串"按原样"编写?感谢所有人的帮助!

答案

正如Schoenobates所回答的那样,解决方案是使用NO_WRAP标志.要添加更多信息,我们在服务器端放置此函数以读取带有标志URL_SAFE的已编码Base64字符串

在php.net中的TOM上获得的函数是:

<?php
    function base64url_decode($base64url)
    {
        $base64 = strtr($base64url, '-_', '+/');
        $plainText = base64_decode($base64);
        return ($plainText);
    }
    ?>
Run Code Online (Sandbox Code Playgroud)

感谢StackOverflow,Axel和Schoenobates为您服务!

小智 36

这将是Android Base64类 - 您需要设置标志以删除换行符:

byte[] image = ...; Base64.encodeToString(image, Base64.NO_WRAP | Base64.URL_SAFE);