• base64编码与解码


    浏览了一下网上的博客,结合自己最近用过的,演示一下org.apache.commons.codec.binary.Base64和osun.misc中的base64编解码,效果一致,实际使用选择一种使用即可

    测试代码如下:

      1 package test.com.dflzm.tpme.szjh;
      2 
      3 import java.io.IOException;
      4 
      5 import org.apache.commons.codec.binary.Base64;
      6 import org.junit.Test;
      7 
      8 import sun.misc.BASE64Decoder;
      9 import sun.misc.BASE64Encoder;
     10 
     11 /**  
     12  * Description
     13  * @author fanj  
     14  * @version 1.0   
     15  * 2019年11月2日 上午11:19:42 
     16  */
     17 public class Base64Test {
     18 
     19     @Test
     20     public void test() {
     21         String str = "2019,70华诞,我爱中国!";
     22         System.out.println("org.apache.commons.codec.binary.Base64");
     23         System.out.println("原字符串:" + str);
     24         // base64编码
     25         String encodeStr = encodeTest(str);
     26         System.out.println("编码后:" + encodeStr);
     27         // 解码
     28         String decodeStr = decodeTest(encodeStr);
     29         System.out.println("解码后:" + decodeStr);
     30         
     31         System.out.println("osun.misc");
     32         byte[] bytes = str.getBytes();
     33         // 编码
     34         String encode = encodeRun(bytes);
     35         System.out.println("编码后:" + encode);
     36         // 解码
     37         String decode = decodeRun(encode);
     38         System.out.println("解码后:" + decode);
     39         
     40         // 判断字符串是否相等
     41         if (encodeStr != null && encodeStr.equals(encode)) {
     42             System.out.println("两种编码方式编码结果一直");
     43         }
     44     }
     45     
     46     /**
     47      * 
     48      * @Description base64编码
     49      * @author fanj
     50      * @date 2019年11月2日
     51      * @param bytes
     52      * @return
     53      */
     54     private String encodeRun(byte[] bytes) {
     55         BASE64Encoder encode = new BASE64Encoder();
     56         String encodeStr = encode.encode(bytes);
     57         return encodeStr;
     58     }
     59     
     60     /**
     61      * 
     62      * @Description base64解码
     63      * @author fanj
     64      * @date 2019年11月2日
     65      * @param str
     66      * @return
     67      */
     68     private String decodeRun(String str) {
     69         BASE64Decoder decode = new BASE64Decoder();
     70         byte[] decodeBuffer = null;
     71         try {
     72             decodeBuffer = decode.decodeBuffer(str);
     73         } catch (IOException e) {
     74             e.printStackTrace();
     75         }
     76         String decodeStr = new String(decodeBuffer);
     77         return decodeStr;
     78     }
     79     
     80     /**
     81      * 
     82      * @Description base64编码
     83      * @author fanj
     84      * @date 2019年11月2日
     85      * @param str
     86      * @return
     87      */
     88     private String encodeTest(String str) {
     89         byte[] encodeByteArray = Base64.encodeBase64(str.getBytes());
     90         String encodeStr = new String(encodeByteArray);
     91         return encodeStr;
     92     }
     93     
     94     /**
     95      * 
     96      * @Description base64解码
     97      * @author fanj
     98      * @date 2019年11月2日
     99      * @param str
    100      * @return
    101      */
    102     private String decodeTest(String str) {
    103         byte[] decodeByteArray = Base64.decodeBase64(str);
    104         String decodeStr = new String(decodeByteArray);
    105         return decodeStr;
    106     }
    107 }

    测试结果如下图:

  • 相关阅读:
    ThreadLocal设计模式 .
    转 Spring @Transactional 声明式事务管理 getCurrentSession
    SQL2008R2 安装提示:”System.Configuration.ConfigurationErrorsException: 创建 userSettings/Microsoft.SqlServe“ 和“安装了 Microsoft Visual Studio 2008 的早期版本“错误的解决办
    根据.mdf文件查看 SQL数据库的版本信息
    带参中文乱码问题 encodeURI和decodeURI
    接收表单参数的几种方法
    HttpClient 解释
    Java annotation 自定义注释@interface的用法
    RedisDesktopManager无法连接虚拟机中启动的redis服务问题解决
    项目里面加入redis单机版 和集群版的配置
  • 原文地址:https://www.cnblogs.com/alphajuns/p/11781310.html
Copyright © 2020-2023  润新知