zoukankan      html  css  js  c++  java
  • urlEncoder和urlDecoder的作用和使用

    1.URLEncoder.encode(String s, String enc) 
    使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式 

    URLDecoder.decode(String s, String enc) 
    使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码。 

    2.发送的时候使用URLEncoder.encode编码,接收的时候使用URLDecoder.decode解码,都按指定的编码格式进行编码、解码,可以保证不会出现乱码

    3.主要用来http get请求不能传输中文参数问题。http请求是不接受中文参数的。

    这就需要发送方,将中文参数encode,接收方将参数decode,这样接收方就能收到准确的原始字符串了。

    如:

    String testString = "abcdefghijk";
            try
            {
                String encoderString = URLEncoder.encode(testString, "utf-8");
                System.out.println(encoderString);
                String decodedString = URLDecoder.decode(encoderString, "utf-8");
                System.out.println(decodedString);
            } catch (UnsupportedEncodingException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    输出:
    abcdefghijk%E6%BD%98%E5%AD%A6%E5%86%9B
    abcdefghijk
  • 相关阅读:
    win10 uwp 商业游戏 1.1.5
    PHP ftp_exec() 函数
    PHP ftp_delete() 函数
    PHP ftp_connect() 函数
    PHP ftp_close() 函数
    PHP ftp_chmod() 函数
    grant 之后是否要跟着 flush privileges
    [TJOI2015]概率论
    win10 uwp 商业游戏 1.1.5
    Total Commander 显示文件包含文件名扩展
  • 原文地址:https://www.cnblogs.com/panxuejun/p/6550131.html
Copyright © 2011-2022 走看看