Java JDK1.7对字符串的BASE64编码解码

package cn.itcast;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.junit.Test;

import sun.misc.BASE64Decoder;

/*
 * @author soto
 * BASE64编码 解码
 * */
public class Demo1 {

    @Test
    public void fun1() throws IOException{
        //BASE64编码  
        String str = "hello";
        byte[] bytes = str.getBytes("utf-8");
        str = new sun.misc.BASE64Encoder().encode(bytes);
        System.out.println("编码后...  "+str);

        //BASE64解码
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = decoder.decodeBuffer(str);

        str = new String(b,"utf-8");
        System.out.println("解码后... "  + str);

    }

}

注意:在jdk1.8 中 的jutil包中加入了新的BASE64解码编码方式,使得编解码更简单.

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值