JAVA-BASE64 encoding and decoding

Source: Internet
Author: User

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

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.