java使用Base64编码 java使用Base64编码实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java使用Base64编码 java使用Base64编码实例

男人要霸气   2021-03-30 我要评论


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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们