当前位置:   article > 正文

base64 编码、解码util_base64codeutil

base64codeutil
  1. import sun.misc.BASE64Encoder;
  2. import java.io.UnsupportedEncodingException;
  3. public class Base64Util {
  4. /**
  5. *
  6. * 将 s 进行 BASE64 编码
  7. * base64 编码、解码util
  8. * 解码 编码字符格式必须一致
  9. * @return String
  10. */
  11. public static String encode(String s) {
  12. if (s == null)
  13. return null;
  14. String res = "";
  15. try {
  16. res = new sun.misc.BASE64Encoder().encode(s.getBytes("utf-8"));
  17. } catch (UnsupportedEncodingException e) {
  18. // TODO Auto-generated catch block
  19. e.printStackTrace();
  20. }
  21. return res;
  22. }
  23. /**
  24. * 将 BASE64 编码的字符串 s 进行解码
  25. *
  26. * @return String
  27. */
  28. public static String decode(String s) {
  29. if (s == null)
  30. return null;
  31. BASE64Decoder decoder = new BASE64Decoder();
  32. try {
  33. byte[] b = decoder.decodeBuffer(s);
  34. return new String(b,"utf-8");
  35. } catch (Exception e) {
  36. return null;
  37. }
  38. }
  39. /**
  40. *
  41. * @return void
  42. */
  43. public static void main(String[] args) {
  44. System.out.println(Base64Util.encode("测试数据"));
  45. System.out.println(Base64Util.decode("uf65/g=="));
  46. }
  47. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/202857
推荐阅读
相关标签
  

闽ICP备14008679号