zoukankan      html  css  js  c++  java
  • Base64

    Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation.

    Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that is designed to deal with textual data.

    用到java包

         <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.10</version>
            </dependency>
            <dependency>
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.4</version>
                <classifier>jdk15</classifier>
            </dependency>

    小样

    import java.io.UnsupportedEncodingException;
    
    import net.sf.json.JSONArray;
    
    import org.apache.commons.codec.binary.Base64;
    
    public class Test {
        public static void main(String[] args) throws UnsupportedEncodingException{
            // 工具
            Base64 base64 = new Base64();
            // 现有byte[]
            byte[] bytes = new byte[] { -25, -69, -76, -27, -97, -70, -25, -103, -66, -25, -89, -111 };
            // 转成字符串,存储或者传输
            String s = base64.encodeAsString(bytes); // 57u05Z+655m+56eR
            // 待使用byte[] 时,将字符串解码
            bytes = base64.decode(s);
            System.out.println(JSONArray.fromObject(bytes).toString());
            // 原始byte[] 是这样的
            System.out.println(new String(bytes, "utf-8"));
        }
    
    }

     打印结果:

    [-25,-69,-76,-27,-97,-70,-25,-103,-66,-25,-89,-111]
    维基百科
  • 相关阅读:
    JSE-1.1.4 内存屏障和CPU缓存
    Ajax
    R手册(Common)--R6 and S4
    掌握 小程序项目新建后的 初始代码 及 git远程管理(2)
    微信小程序 网课学习笔记 开发前的准备工作(1)
    vuex中action如何互相调用
    ajax请求时,请求路径自动拼上页面路径?
    10个免费的CDN
    java面向对象
    java中方法的递归调用
  • 原文地址:https://www.cnblogs.com/zno2/p/4630442.html
Copyright © 2011-2022 走看看