First, Reflection
/*** * Encode by Base64*/ Public StaticString EncodeBase64 (byte[]input]throwsexception{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,Newobject[]{input}); return(String) retobj; } /*** * Decode by Base64*/ Public Static byte[] decodeBase64 (String input)throwsexception{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, useCommons-codec.jar
/** * @parambytes *@return */ Public Static byte[] Decode (Final byte[] bytes) { returnbase64.decodebase64 (bytes); } /*** Binary data encoded as BASE64 String * *@parambytes *@return * @throwsException*/ Public StaticString Encode (Final byte[] bytes) { return NewString (base64.encodebase64 (bytes)); }
Third,
/*** Code *@paramBSTR *@returnString*/ Public StaticString Encode (byte[] BSTR) { return NewSun.misc.BASE64Encoder (). Encode (BSTR); } /*** Decoding *@paramSTR *@returnstring*/ Public Static byte[] Decode (String str) {byte[] bt =NULL; Try{Sun.misc.BASE64Decoder Decoder=NewSun.misc.BASE64Decoder (); BT=Decoder.decodebuffer (str); } Catch(IOException e) {e.printstacktrace (); } returnBT; }
Recommended second, more useful. The Sun pack has a length limitation, but the specific use depends on the situation.
JAVA-BASE64 encoding and decoding