关于UrlEncoder和UrlDecode

关于 UrlEncoder和UrlDecode这两个的含义,主要是针对web项目中url的加码和解码功能

先写个例子

		try {
			String str1 = "?=abdddc?周杰伦%4&9<9,2>";
			String strEncode = URLEncoder.encode(str1, "utf-8");
			System.out.println("编码之后:"+strEncode);
			
			String str2 = "%3F%3Dabdddc%3F%E5%91%A8%E6%9D%B0%E4%BC%A6%254%269%3C9%2C2%3E";
			String strDecode = URLDecoder.decode(str2, "utf-8");
			System.out.println("解码之后:"+strDecode);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

输出结果:

编码之后:%3F%3Dabdddc%3F%E5%91%A8%E6%9D%B0%E4%BC%A6%254%269%3C9%2C2%3E
解码之后:?=abdddc?周杰伦%4&9<9,2>


我们进入到代码里面

URLDecoder.decode

public static String decode(String s, String enc){...}

可以看到该接口的描述部分如下:

    /**
     * Decodes a {@code application/x-www-form-urlencoded} string using a specific
     * encoding scheme.
     * The supplied encoding is used to determine
     * what characters are represented by any consecutive sequences of the
     * form "<i>{@code %xy}</i>".
     */ 

    

Decodes a {@code application/x-www-form-urlencoded} string using a specific encoding scheme 

将application/x-www-form-urlencoded 格式的url地址信息使用指定的编码格式进行解码


The supplied encoding is used to determine what characters are represented by any consecutive sequences of the  form

我们根据提供的字符编码格式来决定将任何url信息解码成对应要呈现的字符串


URLEncoder.encode

public static String encode(String s, String enc)

可以看到该接口的描述部分如下:

    /**
     * Translates a string into {@code application/x-www-form-urlencoded}
     * format using a specific encoding scheme. This method uses the
     * supplied encoding scheme to obtain the bytes for unsafe
     * characters.

    */

Translates a string into {@code application/x-www-form-urlencoded} format using a specific encoding scheme.

通过给定的编码格式将字符串信息加码成我们需要的格式类型。比如:application/x-www-form-urlencoded


 This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.

这个方法使用给定的字符串编码来进行加码,为这些不安全的字符串获取对应的二进制字节码



翻译或者有错误地方,欢迎批评改正。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值