轉載:在 Java 中如何進行 BASE64 編碼和解碼

// 將 s 進行 BASE64 編碼
public static String getBASE64(String s) {
  if (s == null) return null;
  return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
}

// 將 BASE64 編碼的字符串 s 進行解碼
public static String getFromBASE64(String s) {
  if (s == null) return null;
  BASE64Decoder decoder = new BASE64Decoder();
  try {
    byte[] b = decoder.decodeBuffer(s);
    return new String(b);
  } catch (Exception e) {
    return null;
  }
}
發佈了28 篇原創文章 · 獲贊 2 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章