Base64编码格式

package com.xcx.exam.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.util.Base64;
/**

  • @Description: Base64编码测试类 解码前端base64编码格式

  • @Author: liuxiangpeng

  • @CreateDate: 2020/4/15 8:54

  • @UpdateUser: liuxaingpeng

  • @UpdateDate: 2020/4/15 8:54

  • @UpdateRemark: 修改内容

  • @Version: 1.0
    */
    public class ImoocBase64 {
    public static final String toEncode=“admin”;

    /**

    • jdk8以前

    • @throws IOException
      */
      public static void TestBase64Old() throws IOException {
      //编码
      BASE64Encoder encoder=new BASE64Encoder();
      String encoded = encoder.encode(toEncode.getBytes());
      System.out.println(“编码后:”+encoded);

      //解码
      BASE64Decoder decoder=new BASE64Decoder();
      byte[] decoded = decoder.decodeBuffer(encoded);
      System.out.println(“解码后:”+new String(decoded));

    }

    /**

    • jdk1.8后
      /
      public static String TestBase64New(String toEncode){
      //编码
      /String encoded = Base64.getEncoder().encodeToString(toEncode.getBytes());
      System.out.println(“编码后:”+encoded)
      /;
      //解码
      byte[] decoded = java.util.Base64.getDecoder().decode(toEncode.getBytes());
      /
      System.out.println(“解码后:”+new String(decoded));*/
      return new String(decoded);
      }

    public static void main(String[] args) throws IOException {
    /System.out.println(“Before JDK1.8:”);
    TestBase64Old();
    System.out.println(“After JDK1.8:”);
    TestBase64New();
    /
    String s =TestBase64New(“YWRtaW4=”);
    System.out.println(s+"====");
    }
    }

前端参数base64转码:
//获取参数值
var name=KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲user_name").val…("#password").val();
//进行编码
var user = window.btoa(name);
var pasw = window.btoa(pass);

后台接口解码:
//使用base64解码前端参数
ImoocBase64 base = new ImoocBase64();
String usern = base.TestBase64New(user);
String passw= base.TestBase64New(pasw);

你可能感兴趣的:(java,base64,java)