解码和编码
import java.io.IOException;
import org.junit.Test;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 解码和编码
* @version 2016-8-31下午9:01:29
**/
public class TestBase64Encoder {
/**
* 编码
* @param str //要编码的字符串
* @param codeFormat //编码格式
* @throws IOException
*/
@Test
public String encoding(String str, String codeFormat) throws IOException {
BASE64Encoder base64encoder = new BASE64Encoder();
String encode = base64encoder.encode(str.getBytes(codeFormat));
return encode;
}
/**
* 解码
* @param str //要解码的字符串
* @param codeFormat //解码的格式
* @throws IOException
*/
public String decoding(String str, String codeFormat) throws IOException {
//解码
BASE64Decoder decoder = new BASE64Decoder();
byte[] decode = decoder.decodeBuffer(str);
String deco = new String(decode, codeFormat);
return deco;
}
}
import org.junit.Test;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 解码和编码
* @version 2016-8-31下午9:01:29
**/
public class TestBase64Encoder {
/**
* 编码
* @param str //要编码的字符串
* @param codeFormat //编码格式
* @throws IOException
*/
@Test
public String encoding(String str, String codeFormat) throws IOException {
BASE64Encoder base64encoder = new BASE64Encoder();
String encode = base64encoder.encode(str.getBytes(codeFormat));
return encode;
}
/**
* 解码
* @param str //要解码的字符串
* @param codeFormat //解码的格式
* @throws IOException
*/
public String decoding(String str, String codeFormat) throws IOException {
//解码
BASE64Decoder decoder = new BASE64Decoder();
byte[] decode = decoder.decodeBuffer(str);
String deco = new String(decode, codeFormat);
return deco;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.