2019-04-26 14:23:10 2943瀏覽
今天扣丁學堂Java培訓老師給大家詳細介紹一下關(guān)于java使用Base64編碼實例詳解,下面我們一起來看一下吧。
package com.weiwen.provider.utils;
import java.io.IOException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
@Slf4j
public class Base64 {
@Test
public void testBase64() throws IOException {
// BASE64編碼
String s = "1f2bc1970a2eb19aabc0f94acea922717a1ae998603ff0593baff";
BASE64Encoder encoder = new BASE64Encoder();
s = encoder.encode(s.getBytes("UTF-8"));
// System.out.println(s);
log.info("BASE64編碼為:{}", JSON.toJSONString(s));
// BASE64解碼
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer(s);
// System.out.println(new String(bytes, "UTF-8"));
log.info("BASE64解碼為:{}", JSON.toJSONString(new String(bytes, "UTF-8")));
}
}
package com.weiwen.provider.utils;
import java.io.IOException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
@Slf4j
public class Base64 {
/**
* Base64 編碼
* @param encodeText
* @return
* @throws IOException
*/
public static String base64Encode(String encodeText) throws IOException{
BASE64Encoder encoder = new BASE64Encoder();
String str = encoder.encode(encodeText.getBytes("UTF-8"));
log.info("BASE64編碼為:{}", JSON.toJSONString(str));
return str;
}
/**
* Base64 解碼
* @param decodeText
* @return
* @throws IOException
*/
public static byte[] base64Decode(String decodeText) throws IOException{
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer(decodeText);
log.info("BASE64解碼為:{}", JSON.toJSONString(new String(bytes, "UTF-8")));
return bytes;
}
}
【關(guān)注微信公眾號獲取更多學習資料】 【掃碼進入Python全棧開發(fā)免費公開課】
查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>