心安
代码的世界,简单而又直接
posts - 20,comments - 0,views - 6673

懒人菜鸟入门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   逐梦の心安  阅读(698)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示