java使用Base64编码

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

import org.junit.Test;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class Demo1 {
@Test
public void testBase64() throws IOException {

// BASE64编码
String s = "Username";
BASE64Encoder encoder = new BASE64Encoder();
s = encoder.encode(s.getBytes("UTF-8"));
System.out.println(s);

// BASE64解码
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer("dXNlcm5hbWU6");
System.out.println(new String(bytes, "UTF-8"));
}
}

posted @ 2016-09-18 22:16  江山一族  阅读(9910)  评论(0编辑  收藏  举报