---
title: "URLEncoder URLDecoder HtmlUtils"
author: "geelong"
date: "2011-05-08T18:33:02Z"
tags: ["C", "C#", "C++", "HTML", "浏览器"]
categories: ["Web前端"]
slug: "urlencoder-urldecoder-htmlutils"
draft: false
lastmod: "2011-05-08T18:36:30Z"
id: 1036489
url: "https://geelong.iteye.com/blog/1036489"
---

```
	public static void main(String[] args) throws UnsupportedEncodingException {
		String url = "http://www.baidu.com?a=b&b=c&abc=你好";
		String en  = URLEncoder.encode(url, "UTF-8");
		System.out.println(en);
		String decode = URLDecoder.decode(en, "UTF-8");
		System.out.println(decode);
		String decode2 = URLDecoder.decode(decode, "UTF-8");
		System.out.println(decode2);  //2次decode不会再变化
		
		String es = HtmlUtils.htmlEscapeDecimal(url);
		System.out.println(es);  //没有html的特殊符号，不会有变化
		
		String esen = HtmlUtils.htmlEscapeDecimal(en);
		System.out.println(esen);
		
//		http%3A%2F%2Fwww.baidu.com%3Fa%3Db%26b%3Dc%26abc%3D%E4%BD%A0%E5%A5%BD
//		http://www.baidu.com?a=b&b=c&abc=你好
//		http://www.baidu.com?a=b&b=c&abc=你好
//		http://www.baidu.com?a=b&#38;b=c&#38;abc=你好
//		http%3A%2F%2Fwww.baidu.com%3Fa%3Db%26b%3Dc%26abc%3D%E4%BD%A0%E5%A5%BD

	}
```

 

 

 

结论：

1 URLEncoder 会把中文编码为 %ab% 这样的格式，防止中文乱码

2 URLDecoder 解码恢复原样后，继续解码，字符串并不会有别的改变

 

3 HtmlUtils 用于有html特殊的字符转换成转义字符，如 & 转成 &nbsp 等，   如不转换这浏览器解析会出现错误

 

 

 

注意 当对 http:// 进行encode后，浏览器将把它识别为相对路径，而非绝对路径