java使用Base64编码实例
本文实例为大家分享了java使用Base64编码的具体代码,供大家参考,具体内容如下
TestBase64
packagecom.weiwen.provider.utils;
importjava.io.IOException;
importcom.alibaba.fastjson.JSON;
importlombok.extern.slf4j.Slf4j;
importorg.junit.Test;
importsun.misc.BASE64Encoder;
importsun.misc.BASE64Decoder;
@Slf4j
publicclassBase64{
@Test
publicvoidtestBase64()throwsIOException{
//BASE64编码
Strings="1f2bc1970a2eb19aabc0f94acea922717a1ae998603ff0593baff";
BASE64Encoderencoder=newBASE64Encoder();
s=encoder.encode(s.getBytes("UTF-8"));
//System.out.println(s);
log.info("BASE64编码为:{}",JSON.toJSONString(s));
//BASE64解码
BASE64Decoderdecoder=newBASE64Decoder();
byte[]bytes=decoder.decodeBuffer(s);
//System.out.println(newString(bytes,"UTF-8"));
log.info("BASE64解码为:{}",JSON.toJSONString(newString(bytes,"UTF-8")));
}
}
Base64工具类
packagecom.weiwen.provider.utils;
importjava.io.IOException;
importcom.alibaba.fastjson.JSON;
importlombok.extern.slf4j.Slf4j;
importorg.junit.Test;
importsun.misc.BASE64Encoder;
importsun.misc.BASE64Decoder;
@Slf4j
publicclassBase64{
/**
*Base64编码
*@paramencodeText
*@return
*@throwsIOException
*/
publicstaticStringbase64Encode(StringencodeText)throwsIOException{
BASE64Encoderencoder=newBASE64Encoder();
Stringstr=encoder.encode(encodeText.getBytes("UTF-8"));
log.info("BASE64编码为:{}",JSON.toJSONString(str));
returnstr;
}
/**
*Base64解码
*@paramdecodeText
*@return
*@throwsIOException
*/
publicstaticbyte[]base64Decode(StringdecodeText)throwsIOException{
BASE64Decoderdecoder=newBASE64Decoder();
byte[]bytes=decoder.decodeBuffer(decodeText);
log.info("BASE64解码为:{}",JSON.toJSONString(newString(bytes,"UTF-8")));
returnbytes;
}
}
以上所述是小编给大家介绍的java使用Base64编码详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!