欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java使用Base64编码实例

程序员文章站 2024-02-26 21:18:52
本文实例为大家分享了java使用base64编码的具体代码,供大家参考,具体内容如下 test base64 package com.weiwen.prov...

本文实例为大家分享了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编码详解整合,希望对大家有所帮助