base64 编码、解码util

xiaoxiao2021-02-28  150

import sun.misc.BASE64Encoder; import java.io.UnsupportedEncodingException; public class Base64Util { /** * * 将 s 进行 BASE64 编码 * base64 编码、解码util * 解码 编码字符格式必须一致 * @return String */ public static String encode(String s) { if (s == null) return null; String res = ""; try { res = new sun.misc.BASE64Encoder().encode(s.getBytes("utf-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return res; } /** * 将 BASE64 编码的字符串 s 进行解码 * * @return String */ public static String decode(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b,"utf-8"); } catch (Exception e) { return null; } } /** * * @return void */ public static void main(String[] args) { System.out.println(Base64Util.encode("测试数据")); System.out.println(Base64Util.decode("uf65/g==")); } }
转载请注明原文地址: https://www.6miu.com/read-28754.html

最新回复(0)