无法打开从 Android 发送到我的 Intellij 服务器的 Base64 编码图像

Can't open Base64 encoded image sent from Android to my Intellij server

图片保存后就看不到了。

我的Android项目中的Image to Base64方法

public String image2String(ImageView im) {
        Bitmap bitmap = ((BitmapDrawable) im.getDrawable()).getBitmap();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos);
        byte[] bytes = baos.toByteArray();
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }

Intellij 中用于写入文件夹的服务器端方法

try {
            byte[] imageByteArray = java.util.Base64.getMimeDecoder().decode(img);
            FileOutputStream stream = new FileOutputStream("C:\Users\Lau\Desktop\Folder\test.jpg");
            stream.write(imageByteArray);
            stream.close();
        } catch (Exception e) {
            return false;
        }

没有异常抛出,但我无法打开文件夹中的文件。

Window overview

image2Stringreturns以下值:

/9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAIQAABtbnRyUkdC
IFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAA
AADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlk
ZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAA
AChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAA
AAAAAAEAAAAMZW5VUwAAAFgAAAAcAHMAUgBHAEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAA
AAAAAABvogAAOPUAAAOQWFlaIAAAAAAAAGKZAAC3hQAAGNpYWVogAAAAAAAAJKAA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KW7uhre21kDN+9zffrGx1QpqpNJk9oWyhfXCpVO\nB7b7bTJ4mE1aX6Fv+04rut8n/wDMm1+ib/tPnymC3KhnHhJbsNM0zZK1/p9A13XNgP5g3qmth3HG\n6hsTEu2W3cW8aZW+vY2th876qhucj7mWub6VReHyjavbQpxyiMv9XLa2pqUcl4fbMWOrdEPF/oXY\nfr7Y+yfKvPqrsKUMtKjaXVvuUkd+1/ERjjgr0tFUFjBeZrFNBgy1dQxXyjneX5YAFLkpfxj7RMsU\nAA5lvW8i9JeUmCZs54R7oIYmbOL+ke6BrwMezh5JwpZu6PfxIfm/8xsuniqlLN3R7+JD83/mNkja\neIi36rr/AGplZUwATAFgZvO8G5SRP1W49P4mmm6SW6VONFftGV1yFf0x4SO8z36EpTykWz3yaSTI\nJ4KNI+r7zci2ieNK0k6laIt9F7JX7p1Y/cmWhgIGIiW51LGmXLtp4qMo8qqIlbu58hbZaEdaJKyG\nYpxwzz7qkLG8qNpGmzsaMU2kfOyIdq5iHrppe6YaVjfM4NUPz+w7EyumMV5HrfWp10vJyLyslZku\niwAAfAitJeVn+Al7PLR880EQS9nvDN6BuY9LiieXEpZu6PfxIfm/8xsuomK6Cle7o9/Eh+b/AMxs\nkLTxEXDVfzqKypgAmALAzdwbpJvcpx8X8VNLNzk/uW40fiMLrkK/pjwkd5O/TSShFp36aSUIRlIk\nf0FnQtF3Nn5kqY+x3n3VAKxT5ka9Pgz55TNlM6RXyzXWp06dygBZ1ylMqcw/YOD54rthVSlapiAd\nETpHysz9EwZJWz6d28bqtaJdwAHk+VmRz9SfKX7uZlGrayTg3svup8dsAc2XOiy6seegVgS/yaDL\nKXYqAWZcDfHxMm8WeYRqWOKgEde+GVzTfgr9JNh0zv00kj7EY42ukAr7KDU4n//Z\n"

在服务器中 imageByteArray 的长度为 2994 字节,我文件夹中的文件为 2,92 KB(2.994 字节)。

编辑:更好的方法

我们应该以这种方式 ANDROID 编码而不是在服务器中编辑字符串

Base64.encodeToString(bytes, Base64.NO_WRAP);

并在不编辑字符串的情况下以通常的方式在服务器中获取它

java.util.Base64.getMimeDecoder().decode(yourByteArray);

以前的方法:

我只需要编辑我在服务器中获取的字符串,方法是用 img.substring(1, img.length() - 1) 删除它周围的引号,用 img.replace("\n", "").

删除它周围的引号

服务器中的结果代码是这样的:

String fixed = img.substring(1, img.length() - 1).replace("\n", "");
byte[] imageByteArray = java.util.Base64.getMimeDecoder().decode(fixed);
FileOutputStream stream = new FileOutputStream("C:\Users\Lau\Desktop\Folder\test.jpg");
stream.write(imageByteArray);
stream.close();

特别感谢@jps