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

Test Base64
package com.weiwen.provider.utils;
import java.io.IOException;

  1. import com.alibaba.fastjson.JSON;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.junit.Test;
  4. import sun.misc.BASE64Encoder;
  5. import sun.misc.BASE64Decoder;
  6. @Slf4j
  7. public class Base64 {
  8. @Test
  9. public void testBase64() throws IOException {
  10. // BASE64编码
  11. String s = "1f2bc1970a2eb19aabc0f94acea922717a1ae998603ff0593baff";
  12. BASE64Encoder encoder = new BASE64Encoder();
  13. s = encoder.encode(s.getBytes("UTF-8"));
  14. // System.out.println(s);
  15. log.info("BASE64编码为:{}", JSON.toJSONString(s));
  16. // BASE64解码
  17. BASE64Decoder decoder = new BASE64Decoder();
  18. byte[] bytes = decoder.decodeBuffer(s);
  19. // System.out.println(new String(bytes, "UTF-8"));
  20. log.info("BASE64解码为:{}", JSON.toJSONString(new String(bytes, "UTF-8")));
  21. }
  22. }

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编码详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

更多相关文章

  1. Base64编解码Android和ios的例子,补充JNI中的例子
  2. Android(安卓)zip文件压缩解压
  3. Base64编解码Android和ios的例子,补充JNI中的例子
  4. android VideoView的使用例程
  5. InputStream与String/byte[]相互转换
  6. Android中对图像进行Base64编码
  7. ffmpeg支持android硬件解码mediacodec
  8. Android音视频处理之MediaMuxer
  9. Android使用FFMPEG将H264解码为yuv420p

随机推荐

  1. C语言中printf,sprintf和fprintf的区别是
  2. Ruby如何生成随机数
  3. 为什么安全编码标准很重要
  4. 如何编写C程序?C程序的基本编写规则
  5. 什么是C#接口
  6. c语言中什么是转义字符
  7. c++检查两个二进制搜索树是否相同
  8. 什么是Ruby
  9. .NET是什么
  10. 如何在Linux上安装Ruby