JAVA-BASE64 encoding and decoding

Source: Internet
Author: User
Tags base64

Tag:inpu   span   for   invoke    copy     []   bar   access   src   

First, reflection
/*** * Encode by Base64 * /public static String EncodeBase64 (byte[]input) throws exception{ Class clazz =class.forname ("Com.sun.org.apache.xerces.internal.impl.dv.util.Base64"); Method mainmethod= Clazz.getmethod ("encode", byte[].class); Mainmethod.setaccessible (true); Object Retobj=mainmethod.invoke (NULL, New Object[]{input}); Return (String) retobj; /*** * Decode by Base64 * /public static byte[] DecodeBase64 (String input) throws exception{ Class Clazz=class.forname ("Com.sun.org.apache.xerces.internal.impl.dv.util.Base64"); Method mainmethod= Clazz.getmethod ("decode", string.class); Mainmethod.setaccessible (true); Object retobj=mainmethod.invoke (null, input); Return (byte[]) retobj; }
  Second, the use of Commons-codec.jar
/** * @param bytes * @return * * Public static byte[] Decode (final byte[] bytes) { return Base64.decodebase64 (bytes); } /** * Binary data encoded as BASE64 string * * @param bytes * @return * @throws Exception */Public static string encode (final byte[] bytes) { return new String (base64.encodebase64 (bytes)); }
Three
/** * Code * @param BSTR * @return String * /public static string encode (Byte[] BSTR) { return new S Un.misc.BASE64Encoder (). Encode (BSTR); } /** * decode * @param str * @return String * /public static byte[] Decode (string str) { byte[] Bt = null; try { Sun.misc.BASE64Decoder decoder = new Sun.misc.BASE64Decoder (); BT = Decoder.decodebuffer (str); } catch (IOException e) { e.printstacktrace (); } return BT; }

Recommended second, more useful. The Sun pack has a length limitation, but the specific use depends on the situation.

JAVA-BASE64 encoding and decoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.