欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

UrlEncode编码/解码 无编码乱码问题

程序员文章站 2022-06-23 11:12:45
UrlEncode编码/解码 处理乱码UrlEncode在线编码/解密工具http://tool.chinaz.com/tools/urlencode.aspxBase64EncryptUtils 工具类package com.zjy.knife4j.utils;public class Base64EncryptUtils { private final static String[] hex = { "00", "01", "02", "03", "04", "05",...

UrlEncode编码/解码 处理乱码

UrlEncode在线编码/解密工具
http://tool.chinaz.com/tools/urlencode.aspx

Base64EncryptUtils 工具类
package com.zjy.knife4j.utils;

public class Base64EncryptUtils {

    private final static String[] hex = { "00", "01", "02", "03", "04", "05",
            "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F", "10",
            "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B",
            "1C", "1D", "1E", "1F", "20", "21", "22", "23", "24", "25", "26",
            "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", "30", "31",
            "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C",
            "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47",
            "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52",
            "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D",
            "5E", "5F", "60", "61", "62", "63", "64", "65", "66", "67", "68",
            "69", "6A", "6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73",
            "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E",
            "7F", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
            "8A", "8B", "8C", "8D", "8E", "8F", "90", "91", "92", "93", "94",
            "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F",
            "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA",
            "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", "B4", "B5",
            "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", "C0",
            "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB",
            "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6",
            "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1",
            "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC",
            "ED", "EE", "EF", "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7",
            "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF" };

    private final static byte[] val = { 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x01,
            0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
            0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F };

    /**
     * 加密 同样的字符加密结果是一样的
     *
     * @param s
     * @return
     */
    public static String encode(String s) {
        StringBuffer sbuf = new StringBuffer();
        int len = s.length();
        for (int i = 0; i < len; i++) {
            int ch = s.charAt(i);
            if ('A' <= ch && ch <= 'Z') {
                sbuf.append((char) ch);
            } else if ('a' <= ch && ch <= 'z') {
                sbuf.append((char) ch);
            } else if ('0' <= ch && ch <= '9') {
                sbuf.append((char) ch);
            } else if (ch == '-' || ch == '_' || ch == '.' || ch == '!'
                    || ch == '~' || ch == '*' || ch == '\'' || ch == '('
                    || ch == ')') {
                sbuf.append((char) ch);
            } else if (ch <= 0x007F) {
                sbuf.append('%');
                sbuf.append(hex[ch]);
            } else {
                sbuf.append('%');
                sbuf.append('u');
                sbuf.append(hex[(ch >>> 8)]);
                sbuf.append(hex[(0x00FF & ch)]);
            }
        }
        return sbuf.toString();
    }

    /**
     * 解码
     *
     * @param s
     * @return
     */
    public static String decode(String s) {
        StringBuffer sbuf = new StringBuffer();
        int i = 0;
        int len = s.length();
        while (i < len) {
            int ch = s.charAt(i);
            if ('A' <= ch && ch <= 'Z') {
                sbuf.append((char) ch);
            } else if ('a' <= ch && ch <= 'z') {
                sbuf.append((char) ch);
            } else if ('0' <= ch && ch <= '9') {
                sbuf.append((char) ch);
            } else if (ch == '-' || ch == '_' || ch == '.' || ch == '!'
                    || ch == '~' || ch == '*' || ch == '\'' || ch == '('
                    || ch == ')') {
                sbuf.append((char) ch);
            } else if (ch == '%') {
                int cint = 0;
                if ('u' != s.charAt(i + 1)) {
                    cint = (cint << 4) | val[s.charAt(i + 1)];
                    cint = (cint << 4) | val[s.charAt(i + 2)];
                    i += 2;
                } else {
                    cint = (cint << 4) | val[s.charAt(i + 2)];
                    cint = (cint << 4) | val[s.charAt(i + 3)];
                    cint = (cint << 4) | val[s.charAt(i + 4)];
                    cint = (cint << 4) | val[s.charAt(i + 5)];
                    i += 5;
                }
                sbuf.append((char) cint);
            } else {
                sbuf.append((char) ch);
            }
            i++;
        }
        return sbuf.toString();
    }
}

Base64EncryptController
package com.zjy.knife4j.controller;

import com.zjy.knife4j.model.ResultBO;
import com.zjy.knife4j.utils.Base64EncryptUtils;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/url")
@RestController
public class Base64EncryptController {

    /**日志对象*/
    private static final Logger logger = LoggerFactory.getLogger(Base64EncryptController.class);

    @ApiOperation(value = "url加密", notes = "url加密")
    @GetMapping("/encrypt")
    public ResultBO encrypt(@RequestParam(value = "text",required = false) String text){

        logger.info("传入的加密参数为:{}", text);
        ResultBO results = new ResultBO();

        String encrypt = Base64EncryptUtils.encode(text);
        logger.info("加密后的数据为:{}", encrypt);

        results.setCode(200);
        results.setContent(encrypt);
        results.setMsg("调用测试接口成功!");
        results.setSucceed(true);

        logger.info("调用测试接口成功");
        return results;
    }

    @ApiOperation(value = "url解密", notes = "url解密")
    @GetMapping("/decrypt")
    public ResultBO decrypt(@RequestParam(value = "text",required = false) String text){

        logger.info("传入的解密参数为:{}", text);
        ResultBO results = new ResultBO();

        String encrypt = Base64EncryptUtils.decode(text);
        logger.info("解密后的数据为:{}", encrypt);

        results.setCode(200);
        results.setContent(encrypt);
        results.setMsg("调用测试接口成功!");
        results.setSucceed(true);

        logger.info("调用测试接口成功");
        return results;
    }
}

代码粘完了,开始测试
测试入参,随意放数据就行
这里找了个之前物流信息获取的json当入参放入测试

加密入参:

{“status”:“0”,“msg”:“ok”,“result”:{“number”:“780098068058”,“type”:“zto”,“list”:[{“time”:“2018-03-09 11:59:26”,“status”:"【石家庄市】 快件已在 【长安三部】 签收,签收人: 本人, 感谢使用中通快递,期待再次为您服务!"},{“time”:“2018-03-09 09:03:10”,“status”:"【石家庄市】快件已到达【长安三部】(0311-85344265),业务员 容晓光(13081105270)正在第1次派件"},{“time”:“2018-03-08 23:43:44”,“status”:"【石家庄市】 快件离开 【石家庄】 发往 【长安三部】"},{“time”:“2018-03-08 21:00:44”,“status”:"【石家庄市】 快件到达 【石家庄】"},{“time”:“2018-03-07 01:38:45”,“status”:"【广州市】 快件离开 【广州中心】 发往 【石家庄】"},{“time”:“2018-03-07 01:36:53”,“status”:"【广州市】 快件到达 【广州中心】"},{“time”:“2018-03-07 00:40:57”,“status”:"【广州市】 快件离开 【广州花都】 发往 【石家庄中转】"},{“time”:“2018-03-07 00:01:55”,“status”:"【广州市】 【广州花都】(020-37738523) 的 马溪 (18998345739) 已揽收"}],“deliverystatus”:“3”,“issign”:“1”,“expName”:“中通快递”,“expSite”:“www.zto.com”,“expPhone”:“95311”,“courier”:“容晓光”,“courierPhone”:“13081105270”,“updateTime”:“2019-08-27 13:56:19”,“takeTime”:“2天20小时14分”,“logo”:“https://img3.fegine.com/express/zto.jpg”}}

加密结果:

UrlEncode编码/解码 无编码乱码问题

解密入参:

%7B%22status%22%3A%220%22%2C%22msg%22%3A%22ok%22%2C%22result%22%3A%7B%22number%22%3A%22780098068058%22%2C%22type%22%3A%22zto%22%2C%22list%22%3A%5B%7B%22time%22%3A%222018-03-09%2011%3A59%3A26%22%2C%22status%22%3A%22%u3010%u77F3%u5BB6%u5E84%u5E02%u3011%20%u5FEB%u4EF6%u5DF2%u5728%20%u3010%u957F%u5B89%u4E09%u90E8%u3011%20%u7B7E%u6536%2C%u7B7E%u6536%u4EBA%3A%20%u672C%u4EBA%2C%20%u611F%u8C22%u4F7F%u7528%u4E2D%u901A%u5FEB%u9012%2C%u671F%u5F85%u518D%u6B21%u4E3A%u60A8%u670D%u52A1!%22%7D%2C%7B%22time%22%3A%222018-03-09%2009%3A03%3A10%22%2C%22status%22%3A%22%u3010%u77F3%u5BB6%u5E84%u5E02%u3011%u5FEB%u4EF6%u5DF2%u5230%u8FBE%u3010%u957F%u5B89%u4E09%u90E8%u3011%uFF080311-85344265%uFF09%2C%u4E1A%u52A1%u5458%20%u5BB9%u6653%u5149%uFF0813081105270%uFF09%u6B63%u5728%u7B2C1%u6B21%u6D3E%u4EF6%22%7D%2C%7B%22time%22%3A%222018-03-08%2023%3A43%3A44%22%2C%22status%22%3A%22%u3010%u77F3%u5BB6%u5E84%u5E02%u3011%20%u5FEB%u4EF6%u79BB%u5F00%20%u3010%u77F3%u5BB6%u5E84%u3011%20%u53D1%u5F80%20%u3010%u957F%u5B89%u4E09%u90E8%u3011%22%7D%2C%7B%22time%22%3A%222018-03-08%2021%3A00%3A44%22%2C%22status%22%3A%22%u3010%u77F3%u5BB6%u5E84%u5E02%u3011%20%u5FEB%u4EF6%u5230%u8FBE%20%u3010%u77F3%u5BB6%u5E84%u3011%22%7D%2C%7B%22time%22%3A%222018-03-07%2001%3A38%3A45%22%2C%22status%22%3A%22%u3010%u5E7F%u5DDE%u5E02%u3011%20%u5FEB%u4EF6%u79BB%u5F00%20%u3010%u5E7F%u5DDE%u4E2D%u5FC3%u3011%20%u53D1%u5F80%20%u3010%u77F3%u5BB6%u5E84%u3011%22%7D%2C%7B%22time%22%3A%222018-03-07%2001%3A36%3A53%22%2C%22status%22%3A%22%u3010%u5E7F%u5DDE%u5E02%u3011%20%u5FEB%u4EF6%u5230%u8FBE%20%u3010%u5E7F%u5DDE%u4E2D%u5FC3%u3011%22%7D%2C%7B%22time%22%3A%222018-03-07%2000%3A40%3A57%22%2C%22status%22%3A%22%u3010%u5E7F%u5DDE%u5E02%u3011%20%u5FEB%u4EF6%u79BB%u5F00%20%u3010%u5E7F%u5DDE%u82B1%u90FD%u3011%20%u53D1%u5F80%20%u3010%u77F3%u5BB6%u5E84%u4E2D%u8F6C%u3011%22%7D%2C%7B%22time%22%3A%222018-03-07%2000%3A01%3A55%22%2C%22status%22%3A%22%u3010%u5E7F%u5DDE%u5E02%u3011%20%u3010%u5E7F%u5DDE%u82B1%u90FD%u3011%uFF08020-37738523%uFF09%20%u7684%20%u9A6C%u6EAA%20%uFF0818998345739%uFF09%20%u5DF2%u63FD%u6536%22%7D%5D%2C%22deliverystatus%22%3A%223%22%2C%22issign%22%3A%221%22%2C%22expName%22%3A%22%u4E2D%u901A%u5FEB%u9012%22%2C%22expSite%22%3A%22www.zto.com%22%2C%22expPhone%22%3A%2295311%22%2C%22courier%22%3A%22%u5BB9%u6653%u5149%22%2C%22courierPhone%22%3A%2213081105270%22%2C%22updateTime%22%3A%222019-08-27%2013%3A56%3A19%22%2C%22takeTime%22%3A%222%u592920%u5C0F%u65F614%u5206%22%2C%22logo%22%3A%22https%3A%2F%2Fimg3.fegine.com%2Fexpress%2Fzto.jpg%22%7D%7D

解密结果:

UrlEncode编码/解码 无编码乱码问题

控制台打印:
UrlEncode编码/解码 无编码乱码问题

测试OK!

欢迎大神指导,可以留言交流!

======================
本人原创文章,转载注明出入!

=================

本文地址:https://blog.csdn.net/dayonglove2018/article/details/112003252