关于UrlEncoder和UrlDecode

来源:互联网 发布:linux jira破解版下载 编辑:程序博客网 时间:2024/05/18 11:47

关于 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.

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



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