Base64的转码与解码(Java1.8)

来源:互联网 发布:centos安装开发工具包 编辑:程序博客网 时间:2024/06/08 17:47

这个方法是Java 1.8 的,自动导入的Jar包是java.util.Base64

1.解码

public String decode(String s)     {          String decoded= null;          try         {               byte [] barr = Base64.getDecoder().decode(s.getBytes()); //获取到s的字节码,传入进去             decoded= new String(barr, "utf-8");//转码后的字节码转换成String类型,这里字节码就是起到桥接的作用        } catch (Exception e)         {               e.printStackTrace();          }          return decoded;      }  

2.编码

String encoded= Base64.getEncoder().encodeToString(s.getBytes());