BASE64 encoding is a common character encoding, which is used in many places. JDK provides very convenient BASE64Encoder and BASE64Decoder, which can be used to conveniently complete BASE64-based encoding and decoding. The following are two small functions compiled by myself for BASE64 encoding and decoding:
View sourceprint?
01
Import sun. misc. BASE64Encoder;
02
Import sun. misc. BASE64Decoder;
03
04
// Encode s with BASE64
05
PublicstaticString getBASE64 (String s ){
06
If (s = null) returnnull;
07
Return (new sun. misc. BASE64Encoder (). encode (s. getBytes ());
08
}
09
10
// Decodes the BASE64 encoded string s.
11
PublicstaticString getFromBASE64 (String s ){
12
If (s = null) returnnull;
13
BASE64Decoder decoder = new BASE64Decoder ();
14
Try {
15
Byte [] B = decoder. decodeBuffer (s );
16
ReturnnewString (B );
17
} Catch (Exception e ){
18
Returnnull;
19
}
20
}
Author: liweigov