Decode Base64 data in Java

前端 未结 20 1453
时光说笑
时光说笑 2020-11-21 06:04

I have an image that is Base64 encoded. What is the best way to decode that in Java? Hopefully using only the libraries included with Sun Java 6.

20条回答
  •  礼貌的吻别
    2020-11-21 06:38

    Hope this helps you:

    import com.sun.org.apache.xml.internal.security.utils.Base64;
    String str="Hello World";
    String base64_str=Base64.encode(str.getBytes("UTF-8"));
    

    Or:

    String str="Hello World";
    String base64_str="";
    try
       {base64_str=(String)Class.forName("java.util.prefs.Base64").getDeclaredMethod("byteArrayToBase64", new Class[]{byte[].class}).invoke(null, new Object[]{str.getBytes("UTF-8")});
       }
    catch (Exception ee) {}
    

    java.util.prefs.Base64 works on local rt.jar,

    But it is not in The JRE Class White List

    and not in Available classes not listed in the GAE/J white-list

    What a pity!

    PS. In android, it's easy because that android.util.Base64 has been included since Android API Level 8.

提交回复
热议问题