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编码详解整合,希望对大家有所帮助

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值