java使用Base64编码实例

这篇文章主要介绍了java使用Base64编码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文实例为大家分享了java使用Base64编码的具体代码,供大家参考,具体内容如下

Test Base64

 package com.weiwen.provider.utils; import java.io.IOException; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; @Slf4j public class Base64 { @Test public void testBase64() throws IOException { // BASE64编码 String s = "1f2bc1970a2eb19aabc0f94acea922717a1ae998603ff0593baff"; BASE64Encoder encoder = new BASE64Encoder(); s = encoder.encode(s.getBytes("UTF-8")); //   System.out.println(s); log.info("BASE64编码为:{}", JSON.toJSONString(s)); // BASE64解码 BASE64Decoder decoder = new BASE64Decoder(); byte[] bytes = decoder.decodeBuffer(s); //   System.out.println(new String(bytes, "UTF-8")); log.info("BASE64解码为:{}", JSON.toJSONString(new String(bytes, "UTF-8"))); } } 

Base64工具类

 package com.weiwen.provider.utils; import java.io.IOException; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; @Slf4j public class Base64 { /** * Base64 编码 * @param encodeText * @return * @throws IOException */ public static String base64Encode(String encodeText) throws IOException{ BASE64Encoder encoder = new BASE64Encoder(); String str = encoder.encode(encodeText.getBytes("UTF-8")); log.info("BASE64编码为:{}", JSON.toJSONString(str)); return str; } /** * Base64 解码 * @param decodeText * @return * @throws IOException */ public static byte[] base64Decode(String decodeText) throws IOException{ BASE64Decoder decoder = new BASE64Decoder(); byte[] bytes = decoder.decodeBuffer(decodeText); log.info("BASE64解码为:{}", JSON.toJSONString(new String(bytes, "UTF-8"))); return bytes; } } 

以上就是java使用Base64编码实例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java