心安
代码的世界,简单而又直接

懒人菜鸟入门Java系列-习惯性封装常用方法,方便开发过程中调用

  注释: Java版本-1.8

 

  1  * @Author wuwenchao
  2  * @Version 1.0.0
  3  * @Date 2022/4/29 10:07
  4  */
  5 
  6 import java.io.UnsupportedEncodingException;
  7 import java.util.Base64;
  8 
  9 /**
 10  * 封装Java Util包Base64方法
 11  *
 12  * @author wuwenchao
 13  * @Date 2022-04-29 10:07
 14  **/
 15 public class JavaBase64Utils {
 16 
 17     public static final String encodingUTF_8 = "UTF-8";
 18     public static Base64.Encoder encoder;
 19     public static Base64.Decoder decoder;
 20 
 21     static {
 22         decoder = Base64.getDecoder();
 23         encoder = Base64.getEncoder();
 24     }
 25 
 26     /**
 27      * byte[] Base64编码
 28      *
 29      * @Author wuwenchao
 30      * @Date 2022/4/29 10:11
 31      */
 32     public static byte[] encodeBase64(byte[] bytes) {
 33         return encoder.encode(bytes);
 34     }
 35     /**
 36      * 字符串 Base64编码
 37      *
 38      * @Author wuwenchao
 39      * @Date 2022/4/29 10:11
 40      */
 41     public static String encodeBase64(String source) {
 42         byte[] bytes = encodeBase64(source.getBytes());
 43         try {
 44             return new String(bytes, encodingUTF_8);
 45         } catch (UnsupportedEncodingException ex) {
 46             ex.printStackTrace();
 47         }
 48         return  null;
 49     }
 50     /**
 51      * byte[] Base64编码为 字符串
 52      *
 53      * @Author wuwenchao
 54      * @Date 2022/4/29 10:11
 55      */
 56     public static String encodeBase64String(byte[] bytes) {
 57         return encoder.encodeToString(bytes);
 58     }
 59     /**
 60      * 字符串 Base64编码为 byte[]
 61      *
 62      * @Author wuwenchao
 63      * @Date 2022/4/29 10:11
 64      */
 65     public static byte[] encodeBase64Byte(String source) {
 66         byte[] bytes = encodeBase64(source.getBytes());
 67         return  bytes;
 68     }
 69     /**
 70      * Base64Byte 解码
 71      *
 72      * @Author wuwenchao
 73      * @Date 2022/4/29 10:11
 74      */
 75     public static byte[] decodeBase64(byte[] bytes) {
 76         return decoder.decode(bytes);
 77     }
 78     /**
 79      * Base64字符串 解码为 byte[]
 80      *
 81      * @Author wuwenchao
 82      * @Date 2022/4/29 10:11
 83      */
 84     public static byte[] decodeBase64Byte(String string) {
 85         return decoder.decode(string.getBytes());
 86     }
 87     /**
 88      * Base64Byte 解码为字符串
 89      *
 90      * @Author wuwenchao
 91      * @Date 2022/4/29 10:11
 92      */
 93     public static String decodeBase64String(byte[] bytes) {
 94         try {
 95             return new String(decoder.decode(bytes),encodingUTF_8);
 96         } catch (UnsupportedEncodingException e) {
 97             e.printStackTrace();
 98         }
 99         return null;
100     }
101     /**
102      * Base64字符串 解码
103      *
104      * @Author wuwenchao
105      * @Date 2022/4/29 10:11
106      */
107     public static String decodeBase64(String string) {
108         byte[] decode = decodeBase64(string.getBytes());
109         try {
110             return new String(decode, encodingUTF_8);
111         } catch (UnsupportedEncodingException e) {
112             e.printStackTrace();
113         }
114         return null;
115     } 
116 }

 

posted on 2022-04-29 10:46  逐梦の心安  阅读(692)  评论(0编辑  收藏  举报