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