解码和编码

来源:互联网 发布:童鞋淘宝网 编辑:程序博客网 时间:2024/06/05 20:16
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;
    }
    
}
0 0
原创粉丝点击