java使用Base64编码

时间:2023-03-08 17:36:08
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"));
}
}