解碼和編碼

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;
    }
    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章