编码/DECODE BASE64失败



在我的应用程序客户端服务器中,在客户端我以以下格式发送文件内容:

public static String getImageFromURI (Context contesto, Uri uri) {
    InputStream is;
    ByteArrayOutputStream bos;
    try {
        is = contesto.getContentResolver().openInputStream(uri);
        bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        try {
            for (int readNum; (readNum = is.read(buf)) != -1;) {
                bos.write(buf, 0, readNum); //no doubt here is 0
            }
        } catch (IOException ex) {
            Log.d("TAG_F2S", "Sono nel catch IOExcetion con emssage = " + ex.getMessage());
            ex.printStackTrace();
            return null;
        }
        return new String (Base64.encode(bos.toByteArray(), Base64.DEFAULT), "UTF-8");
    } catch (FileNotFoundException fnfe) {
        Log.d("TAG_F2S", "Sono nel catch FileNotFoundExcetion con emssage = " + fnfe.getMessage());
        fnfe.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException uee) {
        Log.d("TAG_F2S", "Sono nel catch UnsupportedEncodingExcetion con emssage = " + uee.getMessage());
        uee.printStackTrace();
        return null;
    }
}

,在服务器端,我尝试创建文件如下:

byte [] byteFile = java.util.Base64.getDecoder ().decode(contenuto.getBytes("UTF-8"));
Files.write(Paths.get(myPath), byteFile);

但我无法获得结果,因为这样的例外:

java.lang.IllegalArgumentException: Illegal base64 character a
at java.util.Base64$Decoder.decode0(Unknown Source)
at java.util.Base64$Decoder.decode(Unknown Source)
....

我的错误是什么?我不明白..感谢您的帮助。


编辑:我发送到服务器的字符串如下:https://codeshare.io/gqqwna

我发现了问题:

当我编码文件内容并在POST请求中将数据发送到服务器时,将修改内容替换Alland仅使用''(Whitespace(的" "字符。在服务器端操作以下操作:

java.util.Base64.getMimeDecoder().decode(contenuto.replace(" ", "+"));

我没有问题。请注意,我使用 getMimedecoder而不是GetDecoder ,否则它不起作用。

有人知道此问题的原因吗?

相关内容

最新更新