JAVA网Java中的URLDecoder类

这是一个用于HTML表单解码的实用程序类。它只执行与URLEncoder类相反的操作,即给定一个编码字符串,它使用指定的方案对其进行解码。在servlet编程中,当使用getParameter()方法访问请求内容时,通常会在返回值之前自动解码这些值。但有时可能需要显式解码URL编码的字符串。 解码字符串时遵循以下步骤:

null
  1. 字母数字字符和某些特殊字符,如’ * ‘, ‘ _ ‘, ‘ “还有” . “没有改变。
  2. + “标志被转换成了空间。
  3. 使用指定的编码方案对所有其他字符进行解码。表单的字符串 %xy ,转换为其编码将导致此三个字符表示的字符。W3C建议使用“UTF-8”进行编码。

例如,编码字符串

u%40geeks+for+geeks

将转换为字符串表示形式,其中%40将替换为@符号,+符号将转换为空格字符。

u@geeks for geeks

方法:

    解码(): 这是该类提供的唯一方法。顾名思义,它返回指定字符串的解码字符串。有一种方法现在已被弃用,它只有一个参数,即要解码的字符串。它不允许您指定使用的编码方案,而是使用平台默认的编码方案。另一个版本允许使用编码规范,因此被广泛使用。

    Syntax :public static String decode(String s)- @Deprecated
    Parameters :
    s : encoded string to be decoded
    
    Syntax :public static String decode(String s,
                String enc)
                         throws UnsupportedEncodingException
    Parameters : 
    s : string to be decoded
    enc : encoding to be used
    Throws :
    UnsupportedEncodingException : If the specified encoding is not used
    

Java实现:

// Java program to show decode() method of
// URLDecoder class
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
public class urlDecoder
{
public static void main(String[] args)
throws UnsupportedEncodingException
{
// encoded string
String encodedString = "u%40geeks+for+geeks" ;
System.out.println( "Encoded String :" );
System.out.println(encodedString);
// decode() method
System.out.println( "Decoded String :" );
System.out.println(URLDecoder.decode(encodedString, "UTF-8" ));
}
}


输出:

Encoded String :
u%40geeks+for+geeks
Decoded String :
u@geeks for geeks

参考资料: 官方Java文档 本文由 Rishabh Mahrsee .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享