简体   繁体   English

Android:如何检查字符串是否为 Base64 编码?

[英]Android: How to check if string is Base64 encoded?

I am not sure how to check if a String is Base64 encoded or not using android.Base64 .我不确定如何使用android.Base64来检查 String 是否是 Base64 编码的。 I know in standard Java using java.util.Base64 calling Base64.getDecoder().decode() would through an IllegalArgumentException if the String wasn't already in Base64, but on android I get no such error, so how can I check if it is Base64 encoded?我知道在标准 Java 中使用java.util.Base64调用Base64.getDecoder().decode()会通过IllegalArgumentException如果字符串不在 Base64 中,但在 android 上我没有得到这样的错误,所以我如何检查它是 Base64 编码的吗?

Add to Gradle:添加到摇篮:

implementation 'commons-codec:commons-codec:1.9'

Add to your code:添加到您的代码中:

if (Base64.isBase64(yourString)) {
   //do stuff
} else {
   //do other stuff
}

try this:尝试这个:

   try{
     Base64.encode("thestringyouwanttocheck".getBytes(),Base64.DEFAULT);
   }catch(IllegalArgumentException e){
      Toast toast=Toast.makeText(context,"Illigal Input",duration);
      toast.show();
   }

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

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