简体   繁体   English

存储在 MySQL 中后的 base64 错误

[英]Bad base64 after storing in MySQL

I am developing a simple app for Max OS X for making screenshots using Swift language.我正在为 Max OS X 开发一个简单的应用程序,用于使用 Swift 语言制作屏幕截图。 After screenshot was made, I convert it into a base64 string using:截图完成后,我使用以下方法将其转换为 base64 字符串:

screenshot.base64EncodedString()

I have a simple .php file on my server for saving this base64 string into MySQL database.我的服务器上有一个简单的 .php 文件,用于将此 base64 字符串保存到 MySQL 数据库中。 Everything works well, but when I am trying to receive this image string from server on my android device, it throws an exception while decoding a base64 "bad base64".一切正常,但是当我尝试从我的 android 设备上的服务器接收此图像字符串时,它在解码 base64“坏 base64”时抛出异常。

This is what I am using to decode image:这是我用来解码图像的内容:

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 

I am using this to put/get image from MySQL database:我正在使用它从 MySQL 数据库中放置/获取图像:

Put: INSERT INTO db (screen) VALUES ('$screen') Get: SELECT * FROM db Put: INSERT INTO db (screen) VALUES ('$screen') Get: SELECT * FROM db

I am storing my base64 as a LONGTEXT in my database.我将我的 base64 作为LONGTEXT存储在我的数据库中。

Can anyone help me?谁能帮我?

The solution is truly replacing "+" with "-" in base64 encoded string before submitting to MySQL and replacing the "-" with "+" after retrieving from MySQL to get the original string.真正的解决方案是在提交到MySQL之前将base64编码字符串中的“+”替换为“-”,并在从MySQL检索到原始字符串后将“-”替换为“+”。 This is because MySQL replaces "+" with white space.这是因为 MySQL 用空格替换了“+”。 It will solve the problem.它将解决问题。

I found a solution! 我找到了解决方案! Replacing "+" with "-" in base64 encoded string solved my problem. 在base64编码的字符串中用“-”替换“ +”解决了我的问题。 The problem was that MySQL replaces + whith a space. 问题是MySQL替换了+空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM