本文實例為大家分享了java使用Base64編碼的具體代碼,供大家參考,具體內容如下
Test 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;@Slf4jpublic 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"))); } }
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;@Slf4jpublic 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; }}
以上所述是小編給大家介紹的java使用Base64編碼詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答
圖片精選