android中base64编码解码,Andorid自带的Base64编解码表

在Android提供的android.util.Base64类中,提供了base64基本的几种编解码格式,源码如下

/**

* Default values for encoder/decoder flags.

*/

public static final int DEFAULT = 0; // 此flag下编码完成的String中会包含换行符\n,+号,末尾以“=”结束

/**

* Encoder flag bit to omit the padding '=' characters at the end

* of the output (if any).

*/

public static final int NO_PADDING = 1; //此flag下最终的编码字符串会包含换行符\n,但是末尾去掉了“=”

/**

* Encoder flag bit to omit all line terminators (i.e., the output

* will be on one long line).

*/

public static final int NO_WRAP = 2; // 此flag下最终编码字符串不包含换行符\n,+号,且字符串全在一行,设置此flag后CRLF flag无效

/**

* Encoder flag bit to indicate lines should be terminated with a

* CRLF pair instead of just an LF. Has no effect if {@code

* NO_WRAP} is specified as well.

*/

public static final int CRLF = 4; // 编码后用CR LF这一对作为一行末尾的换行,而不是unix风格的LF换行

/**

* Encoder/decoder flag bit to indicate using the "URL and

* filename safe" variant of Base64 (see RFC 3548 section 4) where

* {@code -} and {@code _} are used in place of {@code +} and

* {@code /}.

*/

public static final int URL_SAFE = 8; // 编码后,用-代替+,用_代替·/,避免请求是url编码造成的传输问题,包含换行符\n

/**

* Flag to pass to {@link Base64OutputStream} to indicate that it

* should not close the output stream it is wrapping when it

* itself is closed.

*/

public static final int NO_CLOSE = 16;

附上Base64的元码表【from wiki】

59911ca8fc17

image.png

小结

用flag Default编码后,字符串中带有+号和\n换行符,发起网络请求时,当以base64字符串作为参数值传递时,url为了传输安全会把+号全部变成空格,在接收端就会产生各种问题,且前端用的部分的Base64解码库不支持\n,当字符串中含有\n时无法解码还原为图片。所以Base64编码时建议使用NO_WRAP | URL_SAFE,这样可以避免较多问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值