扣丁学堂Java培训之使用Base64编码实例源码详解

2019-04-26 14:23:10 2008浏览

今天扣丁学堂Java培训老师给大家详细介绍一下关于java使用Base64编码实例详解,下面我们一起来看一下吧。



TestBase64

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编码详解整合,希望对大家有所帮助,请关注扣丁学堂Java培训官网、微信等平台,扣丁学堂IT职业在线学习教育有专业的Java讲师为您指导,此外扣丁学堂老师精心推出的Java视频教程定能让你快速掌握Java从入门到精通开发实战技能。扣丁学堂Java技术交流群:850353792。


扣丁学堂微信公众号                          Python全栈开发爬虫人工智能机器学习数据分析免费公开课直播间


      【关注微信公众号获取更多学习资料】         【扫码进入Python全栈开发免费公开课】



查看更多关于“Java开发资讯”的相关文章>>

标签: Java培训 Java视频教程 Java多线程 Java面试题 Java学习视频 Java开发

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号