[关闭]
@File 2020-04-10T03:41:14.000000Z 字数 773 阅读 57

java-base64

骚操作


  1. public class Base64Tool {
  2. private static BASE64Encoder base64Encoder;
  3. private static BASE64Decoder base64Decoder;
  4. static{
  5. base64Encoder = new BASE64Encoder();
  6. base64Decoder = new BASE64Decoder();
  7. }
  8. /**
  9. * byte数组转换base64
  10. * @param bytes byte 数组
  11. * @return base64字符串
  12. */
  13. public static String encode(byte[] bytes){
  14. return base64Encoder.encode(bytes);
  15. }
  16. /**
  17. * 字符串转换base64
  18. * @param str 字符串
  19. * @return base64字符串
  20. */
  21. public static String encode(String str){
  22. return encode(str.getBytes());
  23. }
  24. /**
  25. * base64字符串转字符串
  26. * @param str base64字符串
  27. * @return 字符串
  28. */
  29. public static String decode(String str){
  30. return new String(decodeToByte(str));
  31. }
  32. /**
  33. * base64字符串转byte数组
  34. * @param str 字符串
  35. * @return byte数组
  36. */
  37. public static byte[] decodeToByte(String str){
  38. byte[] bytes = null;
  39. try {
  40. bytes = base64Decoder.decodeBuffer(str);
  41. } catch (IOException e) {
  42. // e.printStackTrace();
  43. }
  44. return bytes;
  45. }
  46. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注