- 一、 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前端:
"font-size: x-large;background-color: darkgreen;color:white;margin: 5px;"> URL在线编码解码 URL编码解码是个啥?
"building" /> "el-common-layout"> "text-area" >margin-top:20px ;">EnCode DeCode width: 40%;height: 200px;margin-left: 30%;margin-top: 20px;"> 编码/解码结果:
{{ myresult }}



