栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

山东大学软件学院2022项目实训——(六)URL解码编码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

山东大学软件学院2022项目实训——(六)URL解码编码

目录
    • 一、 URL编码的原理
    • 二、 URL编码对照表
    • 三、 代码实现

一、 URL编码的原理

当 URL 路径或者查询参数中,带有中文或者特殊字符的时候,就需要对 URL 进行编码(采用十六进制编码格式)。URL 编码的原则是使用安全字符去表示那些不安全的字符。(安全字符,指的是没有特殊用途或者特殊意义的字符。)

URL 中规定了一些具有特殊意义的字符,常被用来分隔两个不同的 URL 组件,这些字符被称为保留字符。例如:
冒号:用于分隔协议和主机组件,斜杠用于分隔主机和路径
?:用于分隔路径和查询参数等。
=用于表示查询参数中的键值对。
&符号用于分隔查询多个键值对。
其余常用的保留字符有:/ . … # @ $ + ; %

URL 之所以需要编码,是因为 URL 中的某些字符会引起歧义,比如 URL 查询参数中包含了”&”或者”%”就会造成服务器解析错误;再比如,URL 的编码格式采用的是 ASCII 码而非 Unicode 格式,这表明 URL 中不允许包含任何非 ASCII 字符(比如中文),否则就会造成 URL 解析错误。

URL 编码协议规定(RFC3986 协议):URL 中只允许使用 ASCII 字符集可以显示的字符,比如英文字母、数字、和- _ . ~ ! *这 6 个特殊字符。当在 URL 中使用不属于 ASCII 字符集的字符时,就要使用特殊的符号对该字符进行编码,比如空格需要用%20来表示。

除了无法显示的字符需要编码外,还需要对 URL 中的部分保留字符和不安全字符进行编码。

二、 URL编码对照表

三、 代码实现

Controller:

package com.sducsrp.csrp.controller.ToolsController.urlEncode;

import com.sducsrp.csrp.common.Constants;
import com.sducsrp.csrp.common.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

@RestController
public class urlEncodeController {
    @RequestMapping("tools/URL/encode")
    public @ResponseBody
    Result encode(@RequestParam("data") String data) {
        System.out.println(data);
        String encodeData = null;
        encodeData = getURLEncoderString(data);
        System.out.println(encodeData);
        Result res = new Result(Constants.CODE_200, null, encodeData);
        return res;
    }

    @RequestMapping("tools/URL/decode")
    public @ResponseBody
    Result decode(@RequestParam("encodeData") String encodeData) {
        System.out.println(encodeData);
        String data = null;
        data = URLDecoderString(encodeData);
        Result res = new Result(Constants.CODE_200, null, data);
        return res;
    }

    //URL编码
    public static String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    //URL解码
    public static String URLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
}

vue前端:







转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/949040.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号