java使用Base64编码实例

网友投稿 213 2023-01-11


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 ordWeLFFg.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

*/

publhttp://ic 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 IOExceptihttp://on{

BASE64Decoder decoder = new BASE64Decoder();

byte[] bytes = decoder.decodeBuffer(decodeText);

log.info("BASE64解码为:{}", JSON.toJSONString(new String(bytes, "UTF-8")));

return bytes;

}

}

以上所述是给大家介绍的java使用Base64编码详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微服务网关的主要功能(微服务架构中服务网关的作用)
下一篇:.net 微服务网关(微服务的网关)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~