精华内容
下载资源
问答
  • url编码、解码工具。 小巧、方便、实用。 解压后直接使用。
  • URL编码转码工具

    2013-01-08 19:51:13
    这是一个URL编码转码工具,具体的功能和演示不发了。 实际功能是把网址转换为%hex 的形式。 1. 这个曾经是安全领域涉及到SQL注入破解屏蔽字符串的方法之一,但是现在已经很少用了 2. 这曾经是入侵大型网站(特别是...
  • 附件内为转码工具,包含url转码,unicode 转码等类型,解压密码1024,如果可以的话麻烦给出使用意见幺,
  • 一、encodeURIComponent()1.encodeURIComponent()方法的使用语法:encodeURIComponent(URIstring)参数:URIstring,必需。一个字符串,含有 URI 组件或其他要编码的文本。2.encodeURIComponent()测试测试document....

    一、encodeURIComponent()

    1.encodeURIComponent()方法的使用

    语法:encodeURIComponent(URIstring)

    参数:URIstring,必需。一个字符串,含有 URI 组件或其他要编码的文本。

    2.encodeURIComponent()测试

    测试

    document.write(encodeURIComponent("https://www.sojson.com/encodeurl.html?我是个中文参数"))

    document.write("
    ")

    document.write(encodeURIComponent(",/?:@&=+$#"))

    结果

    http%3A%2F%2Fwww.sojson.com%2Fencodeurl.html%3F%E6%88%91%E6%98%AF%E4%B8%AA%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0

    %3Cbr%20%2F%3E

    %2C%2F%3F%3A%40%26%3D%2B%24%23

    二、encodeURI()测试

    测试

    document.write(encodeURI("https://www.sojson.com/encodeurl.html?我是个中文参数"))

    document.write("
    ")

    document.write(encodeURI(",/?:@&=+$#"))

    结果

    https://www.sojson.com/encodeurl.html?%E6%88%91%E6%98%AF%E4%B8%AA%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0

    %3Cbr%20/%3E

    ,/?:@&=+$#

    三、 encodeURIComponent() 函数 与 encodeURI() 函数的区别

    请注意 encodeURIComponent() 函数 与 encodeURI() 函数的区别之处,前者假定它的参数是 URI 的一部分(比如协议、主机名、路径或查询字符串)。因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。

    广告 for Goolge

    展开全文
  • 此文仅对自己工作中用到的类进行总结,方便以后的使用。 ... import java.io.... * url转码、解码 * * @author * @date */ public class UrlUtil { private final static String ENCODE

    此文仅对自己工作中用到的类进行总结,方便以后的使用。

    package com.cn.hnust.util;
    
    import java.io.UnsupportedEncodingException;
    import java.util.HashMap;
    import java.util.Map;
    /**
     * url转码、解码
     *
     * @author
     * @date 
     */
    public class UrlUtil {
        private final static String ENCODE = "GBK"; 
        /**
         * URL 解码
         *
         * @return String
         * @author lifq
         * @date 2015-3-17 下午04:09:51
         */
        public static String getURLDecoderString(String str) {
            String result = "";
            if (null == str) {
                return "";
            }
            try {
                result = java.net.URLDecoder.decode(str, ENCODE);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return result;
        }
        /**
         * URL 转码
         *
         * @return String
         * @author lifq
         * @date 2015-3-17 下午04:10:28
         */
        public static String getURLEncoderString(String str) {
            String result = "";
            if (null == str) {
                return "";
            }
            try {
                result = java.net.URLEncoder.encode(str, ENCODE);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return result;
         }
        /**  
         * 正则替换  
         * @param url  
         * @param name  
         * @param value  
         * @return  
         */  
        public static String replaceUrlParameterReg(String url, String name, String value) {  
            if (StringUtils.isNotBlank(url) && StringUtils.isNotBlank(value)) {  
                url = url.replaceAll("(" + name + "=[^&]*)", name + "=" + value);  
            }  
            return url;  
        }  
        /**  
         * 正则替换  
         * @param url  
         * @param name  
         * @param value  
         * @return  
         */  
        public static String getUrlParameterReg(String url, String name) {
    
            Map<String, String> mapRequest = new HashMap<String, String>();
    
              String[] arrSplit=null;
    
            String strUrlParam=TruncateUrlPage(url);
            if(strUrlParam==null)
            {
                return "";
            }
              //每个键值为一组 www.2cto.com
            arrSplit=strUrlParam.split("[&]");
            for(String strSplit:arrSplit)
            {
                  String[] arrSplitEqual=null;         
                  arrSplitEqual= strSplit.split("[=]");
    
                  //解析出键值
                  if(arrSplitEqual.length>1)
                  {
                      //正确解析
                      mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
    
                  }
                  else
                  {
                      if(arrSplitEqual[0]!="")
                      {
                      //只有参数没有值,不加入
                      mapRequest.put(arrSplitEqual[0], "");       
                      }
                  }
            }   
            return mapRequest.get(name);  
        }  
        /**
         * 去掉url中的路径,留下请求参数部分
         * @param strURL url地址
         * @return url请求参数部分
         */
        private static String TruncateUrlPage(String strURL)
        {
        String strAllParam=null;
          String[] arrSplit=null;
    
          strURL=strURL.trim().toLowerCase();
    
          arrSplit=strURL.split("[?]");
          if(strURL.length()>1)
          {
              if(arrSplit.length>1)
              {
                      if(arrSplit[1]!=null)
                      {
                      strAllParam=arrSplit[1];
                      }
              }
          }
    
        return strAllParam;   
        }
        public static void main(String[] args) {
            String str ="/activePage?wxto=0C29975119DE037B4F25CD119CF4CF11&activity2018topid=oz9Zj07Jg5O9IfAyVauT-GL-CSfM&activity2018topunid=o-NUA0l8fxm2_5fmK_ocpyxrP1dE&subscribe=0&timestamp=1516008942920&-NUA0l8fxm2_5fmK_ocpyxrP1dE&oaccesstoken=5_xJYY3gnUTD4pqJFc7_ltbvclb0ZRKOFBEc5BXAwi7vjZndRAN58AkPazqUJDGzBbrw-J1ag1PF0C-S39SqJflA&m=1&wxt=C9A58DA3804E46981BF4B78C8C862743&-NUA0oHjJjCcyGe-wJdEFYjwrfU&-NUA0oHjJjCcyGe-wJdEFYjwrfU&-NUA0oHjJjCcyGe-wJdEFYjwrfU";
            String m = "9j70Dj%2BpVA64qK9%2FeOjsg2dP3biHh6imIC6WFXtIRcR6nHwDkjHPNOL9zUi7fmpcbgJP3u%2Bh3VUTfDO0B2fsbKWsizlqOKdXJovkg3HZMTxAIcByvYvtHoCyd9iOgUxoDqslzoMunvnEzPS8FbYc%2Fjkd4y47zhKk%2FUTE4GGm8ZM5Ns5eYwZSFL7A8pX0U%2BXfsMR%2FGf5z2pci6a5BJaki4e0z0Pmh5sPsjytmKdjdssHQ98iZ8ge3mXJm7G%2FaSJ3e%2FHvNENLv0JRJf7q1bFu180RZEtsMRs%2BWdesTuJamtJBYtLxQEYaCMygRcmd%2F1VdSqfLnuZqcZAzT6GAmvBwZyfzCK9LffGsyZGWce%2B0g2R%2FTCKTTtNpWWCJb0e7Gs0g82DmBNV4jh3hrOAmeLNwpu0M%2B049%2FHrDxAZPvXowCb5OLb7if%2BVqBpYkphGssurkqLV2KTXx3vhjl9vIiMQnD4397oNYf81P%2BBWFPBFhPQSfGaxZGZbAlPgkluR4%2Fj75f9SeZlcWFMnl%2BYKYtopw1gw%3D%3D";
            System.out.println(getURLEncoderString(m));
            System.out.println(getURLDecoderString(m));
          //System.out.println( getUrlParameterReg(str,"wxto"));
        }
    }
    
    

     

    展开全文
  • url 中含有的中文进行转码操作一般情况下,将带有中文的 url 拷贝到开发工具,开发工具都会有相应的转码(自动转码),现在大部分的浏览器也可以对含有中文的 url 进行转码(自动转码)情景说明android安卓系统,能够...

    对 url 中含有的中文进行转码操作

    一般情况下,将带有中文的 url 拷贝到开发工具,开发工具都会有相应的转码(自动转码),

    现在大部分的浏览器也可以对含有中文的 url 进行转码(自动转码)

    情景说明

    android

    安卓系统,能够识别中文路径:

    https://spdbimg.idoupiao.com/film/my/美国队长3

    IOS

    苹果系统,不能够识别中文路径:

    https://spdbimg.idoupiao.com/film/my/美国队长3

    必须将以上路径进行转码为下来形式才可以访问:

    https://spdbimg.idoupiao.com/film/my/%E7%BE%8E%E5%9B%BD%E9%98%9F%E9%95%BF3

    这和他们各自的战略定位有关系。。。

    编码实现

    1、先寻找有没有已经实现类似功能的成熟工具。

    2、找到 JDK 提供的 URLEncoder 类的 encode 方法基本满足需求。

    3、测试是否满足当下需求。

    经过测试发现,JDK 提供的 URLEncoder 类的 encode 方法会将 ':' 和 '/' 等字符也进行编码。

    4、查找 JDK 提供的 URLEncoder 类的 encode 方法 相关源码。

    5、发现可以进行部分改造满足当前需求,重写 URLEncoder 类,并将其设置为私有静态类,多人合作时,防止他人误用。

    6、改造完成进行验证型测试。

    添加以下两条语句:

    // 排除这两个特殊字符的编码

    dontNeedEncoding.set(':');

    dontNeedEncoding.set('/');

    重写后的 URLEncoder 类:

    /**

    * Utility class for HTML form encoding. This class contains static methods

    * for converting a String to the application/x-www-form-urlencoded MIME

    * format. For more information about HTML form encoding, consult the HTML

    * specification.

    *

    *

    * When encoding a String, the following rules apply:

    *

    *

    *

    The alphanumeric characters "{@code a}" through

    * "{@code z}", "{@code A}" through

    * "{@code Z}" and "{@code 0}"

    * through "{@code 9}" remain the same.

    *

    The special characters "{@code .}",

    * "{@code -}", "{@code *}", and

    * "{@code _}" remain the same.

    *

    The space character "   " is

    * converted into a plus sign "{@code +}".

    *

    All other characters are unsafe and are first converted into

    * one or more bytes using some encoding scheme. Then each byte is

    * represented by the 3-character string

    * "{@code %xy}", where xy is the

    * two-digit hexadecimal representation of the byte.

    * The recommended encoding scheme to use is UTF-8. However,

    * for compatibility reasons, if an encoding is not specified,

    * then the default encoding of the platform is used.

    *

    *

    *

    * For example using UTF-8 as the encoding scheme the string "The

    * string ü@foo-bar" would get converted to

    * "The+string+%C3%BC%40foo-bar" because in UTF-8 the character

    * ü is encoded as two bytes C3 (hex) and BC (hex), and the

    * character @ is encoded as one byte 40 (hex).

    *

    * @author Herb Jellinek

    * @since JDK1.0

    */

    private static class URLEncoder {

    static BitSet dontNeedEncoding;

    static final int caseDiff = ('a' - 'A');

    static String dfltEncName = null;

    static {

    /* The list of characters that are not encoded has been

    * determined as follows:

    *

    * RFC 2396 states:

    * -----

    * Data characters that are allowed in a URI but do not have a

    * reserved purpose are called unreserved. These include upper

    * and lower case letters, decimal digits, and a limited set of

    * punctuation marks and symbols.

    *

    * unreserved = alphanum | mark

    *

    * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"

    *

    * Unreserved characters can be escaped without changing the

    * semantics of the URI, but this should not be done unless the

    * URI is being used in a context that does not allow the

    * unescaped character to appear.

    * -----

    *

    * It appears that both Netscape and Internet Explorer escape

    * all special characters from this list with the exception

    * of "-", "_", ".", "*". While it is not clear why they are

    * escaping the other characters, perhaps it is safest to

    * assume that there might be contexts in which the others

    * are unsafe if not escaped. Therefore, we will use the same

    * list. It is also noteworthy that this is consistent with

    * O'Reilly's "HTML: The Definitive Guide" (page 164).

    *

    * As a last note, Intenet Explorer does not encode the "@"

    * character which is clearly not unreserved according to the

    * RFC. We are being consistent with the RFC in this matter,

    * as is Netscape.

    *

    */

    dontNeedEncoding = new BitSet(256);

    int i;

    for (i = 'a'; i <= 'z'; i++) {

    dontNeedEncoding.set(i);

    }

    for (i = 'A'; i <= 'Z'; i++) {

    dontNeedEncoding.set(i);

    }

    for (i = '0'; i <= '9'; i++) {

    dontNeedEncoding.set(i);

    }

    dontNeedEncoding.set(' '); /* encoding a space to a + is done

    * in the encode() method */

    dontNeedEncoding.set('-');

    dontNeedEncoding.set('_');

    dontNeedEncoding.set('.');

    dontNeedEncoding.set('*');

    // 排除这两个特殊字符的编码

    dontNeedEncoding.set(':');

    dontNeedEncoding.set('/');

    dfltEncName = AccessController.doPrivileged(

    new GetPropertyAction("file.encoding")

    );

    }

    /**

    * You can't call the constructor.

    */

    private URLEncoder() { }

    /**

    * Translates a string into {@code x-www-form-urlencoded}

    * format. This method uses the platform's default encoding

    * as the encoding scheme to obtain the bytes for unsafe characters.

    *

    * @param s {@code String} to be translated.

    * @deprecated The resulting string may vary depending on the platform's

    * default encoding. Instead, use the encode(String,String)

    * method to specify the encoding.

    * @return the translated {@code String}.

    */

    @Deprecated

    public static String encode(String s) {

    String str = null;

    try {

    str = encode(s, dfltEncName);

    } catch (UnsupportedEncodingException e) {

    // The system should always have the platform default

    }

    return str;

    }

    /**

    * 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.

    *

    * Note: The

    * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">

    * World Wide Web Consortium Recommendation

    states that

    * UTF-8 should be used. Not doing so may introduce

    * incompatibilities.

    *

    * @param s {@code String} to be translated.

    * @param enc The name of a supported

    * character

    * encoding.

    * @return the translated {@code String}.

    * @exception UnsupportedEncodingException

    * If the named encoding is not supported

    * @see URLDecoder#decode(java.lang.String, java.lang.String)

    * @since 1.4

    */

    public static String encode(String s, String enc)

    throws UnsupportedEncodingException {

    boolean needToChange = false;

    StringBuffer out = new StringBuffer(s.length());

    Charset charset;

    CharArrayWriter charArrayWriter = new CharArrayWriter();

    if (enc == null)

    throw new NullPointerException("charsetName");

    try {

    charset = Charset.forName(enc);

    } catch (IllegalCharsetNameException e) {

    throw new UnsupportedEncodingException(enc);

    } catch (UnsupportedCharsetException e) {

    throw new UnsupportedEncodingException(enc);

    }

    for (int i = 0; i < s.length();) {

    int c = (int) s.charAt(i);

    //System.out.println("Examining character: " + c);

    if (dontNeedEncoding.get(c)) {

    if (c == ' ') {

    c = '+';

    needToChange = true;

    }

    //System.out.println("Storing: " + c);

    out.append((char)c);

    i++;

    } else {

    // convert to external encoding before hex conversion

    do {

    charArrayWriter.write(c);

    /*

    * If this character represents the start of a Unicode

    * surrogate pair, then pass in two characters. It's not

    * clear what should be done if a bytes reserved in the

    * surrogate pairs range occurs outside of a legal

    * surrogate pair. For now, just treat it as if it were

    * any other character.

    */

    if (c >= 0xD800 && c <= 0xDBFF) {

    /*

    System.out.println(Integer.toHexString(c)

    + " is high surrogate");

    */

    if ( (i+1) < s.length()) {

    int d = (int) s.charAt(i+1);

    /*

    System.out.println("\tExamining "

    + Integer.toHexString(d));

    */

    if (d >= 0xDC00 && d <= 0xDFFF) {

    /*

    System.out.println("\t"

    + Integer.toHexString(d)

    + " is low surrogate");

    */

    charArrayWriter.write(d);

    i++;

    }

    }

    }

    i++;

    } while (i < s.length() && !dontNeedEncoding.get((c = (int) s.charAt(i))));

    charArrayWriter.flush();

    String str = new String(charArrayWriter.toCharArray());

    byte[] ba = str.getBytes(charset);

    for (int j = 0; j < ba.length; j++) {

    out.append('%');

    char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);

    // converting to use uppercase letter as part of

    // the hex value if ch is a letter.

    if (Character.isLetter(ch)) {

    ch -= caseDiff;

    }

    out.append(ch);

    ch = Character.forDigit(ba[j] & 0xF, 16);

    if (Character.isLetter(ch)) {

    ch -= caseDiff;

    }

    out.append(ch);

    }

    charArrayWriter.reset();

    needToChange = true;

    }

    }

    return (needToChange? out.toString() : s);

    }

    }

    测试代码:

    @Test

    public void testReplace() throws UnsupportedEncodingException {

    // https://ojjqzz83h.qnssl.com/film/my/美国队长3?imageView2/1/w/140/h/90/interlace/0/q/100

    // https://spdbimg.idoupiao.com/film/my/%E7%BE%8E%E5%9B%BD%E9%98%9F%E9%95%BF3

    String oldImgUrl = "https://spdbimg.idoupiao.com/film/my/美国队长3";

    oldImgUrl = URLEncoder.encode(oldImgUrl, "UTF-8");

    // oldImgUrl = new String(Base64.getEncoder().encode(oldImgUrl.getBytes()));

    String oldDomain = "spdbimg.idoupiao.com";

    String newDomain = "ojjqzz83h.qnssl.com";

    String suffix = "?imageView2/1/w/140/h/90/interlace/0/q/100";

    String newImgUrl = StringUtils.replace(oldImgUrl, oldDomain, newDomain).concat(suffix);

    System.out.println("newImgUrl === " + newImgUrl);

    }

    完整代码如下:

    package com.github.ljmatlight.util;

    import org.junit.Test;

    import sun.security.action.GetPropertyAction;

    import java.io.CharArrayWriter;

    import java.io.UnsupportedEncodingException;

    import java.net.URLDecoder;

    import java.nio.charset.Charset;

    import java.nio.charset.IllegalCharsetNameException;

    import java.nio.charset.UnsupportedCharsetException;

    import java.security.AccessController;

    import java.util.BitSet;

    /**

    * Description:对 url 中含有的中文进行转码操作

    *
    Author:ljmatlight

    */

    public class StringTest {

    @Test

    public void testReplace() throws UnsupportedEncodingException {

    // https://ojjqzz83h.qnssl.com/film/my/美国队长3?imageView2/1/w/140/h/90/interlace/0/q/100

    // https://spdbimg.idoupiao.com/film/my/%E7%BE%8E%E5%9B%BD%E9%98%9F%E9%95%BF3

    String oldImgUrl = "https://spdbimg.idoupiao.com/film/my/美国队长3";

    oldImgUrl = URLEncoder.encode(oldImgUrl, "UTF-8");

    // oldImgUrl = new String(Base64.getEncoder().encode(oldImgUrl.getBytes()));

    String oldDomain = "spdbimg.idoupiao.com";

    String newDomain = "ojjqzz83h.qnssl.com";

    String suffix = "?imageView2/1/w/140/h/90/interlace/0/q/100";

    String newImgUrl = StringUtils.replace(oldImgUrl, oldDomain, newDomain).concat(suffix);

    System.out.println("newImgUrl === " + newImgUrl);

    }

    /**

    * Utility class for HTML form encoding. This class contains static methods

    * for converting a String to the application/x-www-form-urlencoded MIME

    * format. For more information about HTML form encoding, consult the HTML

    * specification.

    *

    *

    * When encoding a String, the following rules apply:

    *

    *

    *

    The alphanumeric characters "{@code a}" through

    * "{@code z}", "{@code A}" through

    * "{@code Z}" and "{@code 0}"

    * through "{@code 9}" remain the same.

    *

    The special characters "{@code .}",

    * "{@code -}", "{@code *}", and

    * "{@code _}" remain the same.

    *

    The space character "   " is

    * converted into a plus sign "{@code +}".

    *

    All other characters are unsafe and are first converted into

    * one or more bytes using some encoding scheme. Then each byte is

    * represented by the 3-character string

    * "{@code %xy}", where xy is the

    * two-digit hexadecimal representation of the byte.

    * The recommended encoding scheme to use is UTF-8. However,

    * for compatibility reasons, if an encoding is not specified,

    * then the default encoding of the platform is used.

    *

    *

    *

    * For example using UTF-8 as the encoding scheme the string "The

    * string ü@foo-bar" would get converted to

    * "The+string+%C3%BC%40foo-bar" because in UTF-8 the character

    * ü is encoded as two bytes C3 (hex) and BC (hex), and the

    * character @ is encoded as one byte 40 (hex).

    *

    * @author Herb Jellinek

    * @since JDK1.0

    */

    private static class URLEncoder {

    static BitSet dontNeedEncoding;

    static final int caseDiff = ('a' - 'A');

    static String dfltEncName = null;

    static {

    /* The list of characters that are not encoded has been

    * determined as follows:

    *

    * RFC 2396 states:

    * -----

    * Data characters that are allowed in a URI but do not have a

    * reserved purpose are called unreserved. These include upper

    * and lower case letters, decimal digits, and a limited set of

    * punctuation marks and symbols.

    *

    * unreserved = alphanum | mark

    *

    * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"

    *

    * Unreserved characters can be escaped without changing the

    * semantics of the URI, but this should not be done unless the

    * URI is being used in a context that does not allow the

    * unescaped character to appear.

    * -----

    *

    * It appears that both Netscape and Internet Explorer escape

    * all special characters from this list with the exception

    * of "-", "_", ".", "*". While it is not clear why they are

    * escaping the other characters, perhaps it is safest to

    * assume that there might be contexts in which the others

    * are unsafe if not escaped. Therefore, we will use the same

    * list. It is also noteworthy that this is consistent with

    * O'Reilly's "HTML: The Definitive Guide" (page 164).

    *

    * As a last note, Intenet Explorer does not encode the "@"

    * character which is clearly not unreserved according to the

    * RFC. We are being consistent with the RFC in this matter,

    * as is Netscape.

    *

    */

    dontNeedEncoding = new BitSet(256);

    int i;

    for (i = 'a'; i <= 'z'; i++) {

    dontNeedEncoding.set(i);

    }

    for (i = 'A'; i <= 'Z'; i++) {

    dontNeedEncoding.set(i);

    }

    for (i = '0'; i <= '9'; i++) {

    dontNeedEncoding.set(i);

    }

    dontNeedEncoding.set(' '); /* encoding a space to a + is done

    * in the encode() method */

    dontNeedEncoding.set('-');

    dontNeedEncoding.set('_');

    dontNeedEncoding.set('.');

    dontNeedEncoding.set('*');

    // 排除这两个特殊字符的编码

    dontNeedEncoding.set(':');

    dontNeedEncoding.set('/');

    dfltEncName = AccessController.doPrivileged(

    new GetPropertyAction("file.encoding")

    );

    }

    /**

    * You can't call the constructor.

    */

    private URLEncoder() { }

    /**

    * Translates a string into {@code x-www-form-urlencoded}

    * format. This method uses the platform's default encoding

    * as the encoding scheme to obtain the bytes for unsafe characters.

    *

    * @param s {@code String} to be translated.

    * @deprecated The resulting string may vary depending on the platform's

    * default encoding. Instead, use the encode(String,String)

    * method to specify the encoding.

    * @return the translated {@code String}.

    */

    @Deprecated

    public static String encode(String s) {

    String str = null;

    try {

    str = encode(s, dfltEncName);

    } catch (UnsupportedEncodingException e) {

    // The system should always have the platform default

    }

    return str;

    }

    /**

    * 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.

    *

    * Note: The

    * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">

    * World Wide Web Consortium Recommendation

    states that

    * UTF-8 should be used. Not doing so may introduce

    * incompatibilities.

    *

    * @param s {@code String} to be translated.

    * @param enc The name of a supported

    * character

    * encoding.

    * @return the translated {@code String}.

    * @exception UnsupportedEncodingException

    * If the named encoding is not supported

    * @see URLDecoder#decode(java.lang.String, java.lang.String)

    * @since 1.4

    */

    public static String encode(String s, String enc)

    throws UnsupportedEncodingException {

    boolean needToChange = false;

    StringBuffer out = new StringBuffer(s.length());

    Charset charset;

    CharArrayWriter charArrayWriter = new CharArrayWriter();

    if (enc == null)

    throw new NullPointerException("charsetName");

    try {

    charset = Charset.forName(enc);

    } catch (IllegalCharsetNameException e) {

    throw new UnsupportedEncodingException(enc);

    } catch (UnsupportedCharsetException e) {

    throw new UnsupportedEncodingException(enc);

    }

    for (int i = 0; i < s.length();) {

    int c = (int) s.charAt(i);

    //System.out.println("Examining character: " + c);

    if (dontNeedEncoding.get(c)) {

    if (c == ' ') {

    c = '+';

    needToChange = true;

    }

    //System.out.println("Storing: " + c);

    out.append((char)c);

    i++;

    } else {

    // convert to external encoding before hex conversion

    do {

    charArrayWriter.write(c);

    /*

    * If this character represents the start of a Unicode

    * surrogate pair, then pass in two characters. It's not

    * clear what should be done if a bytes reserved in the

    * surrogate pairs range occurs outside of a legal

    * surrogate pair. For now, just treat it as if it were

    * any other character.

    */

    if (c >= 0xD800 && c <= 0xDBFF) {

    /*

    System.out.println(Integer.toHexString(c)

    + " is high surrogate");

    */

    if ( (i+1) < s.length()) {

    int d = (int) s.charAt(i+1);

    /*

    System.out.println("\tExamining "

    + Integer.toHexString(d));

    */

    if (d >= 0xDC00 && d <= 0xDFFF) {

    /*

    System.out.println("\t"

    + Integer.toHexString(d)

    + " is low surrogate");

    */

    charArrayWriter.write(d);

    i++;

    }

    }

    }

    i++;

    } while (i < s.length() && !dontNeedEncoding.get((c = (int) s.charAt(i))));

    charArrayWriter.flush();

    String str = new String(charArrayWriter.toCharArray());

    byte[] ba = str.getBytes(charset);

    for (int j = 0; j < ba.length; j++) {

    out.append('%');

    char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);

    // converting to use uppercase letter as part of

    // the hex value if ch is a letter.

    if (Character.isLetter(ch)) {

    ch -= caseDiff;

    }

    out.append(ch);

    ch = Character.forDigit(ba[j] & 0xF, 16);

    if (Character.isLetter(ch)) {

    ch -= caseDiff;

    }

    out.append(ch);

    }

    charArrayWriter.reset();

    needToChange = true;

    }

    }

    return (needToChange? out.toString() : s);

    }

    }

    }

    作者:随风浮云

    出处:http://www.cnblogs.com/ljmatlight

    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明。

    文中有不妥或者错误的地方,欢迎勘误,如果你有更好的建议,可以给我留言讨论,共同进步。

    互联网技术时效性较强,引用请慎重。

    展开全文
  • import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;/*** url转码、解码** @author* @date*/public class UrlUtil {private final static String EN...

    package com.cn.hnust.util;

    import java.io.UnsupportedEncodingException;

    import java.util.HashMap;

    import java.util.Map;

    /**

    * url转码、解码

    *

    * @author

    * @date

    */

    public class UrlUtil {

    private final static String ENCODE = "GBK";

    /**

    * URL 解码

    *

    * @return String

    * @author lifq

    * @date 2015-3-17 下午04:09:51

    */

    public static String getURLDecoderString(String str) {

    String result = "";

    if (null == str) {

    return "";

    }

    try {

    result = java.net.URLDecoder.decode(str, ENCODE);

    } catch (UnsupportedEncodingException e) {

    e.printStackTrace();

    }

    return result;

    }

    /**

    * URL 转码

    *

    * @return String

    * @author lifq

    * @date 2015-3-17 下午04:10:28

    */

    public static String getURLEncoderString(String str) {

    String result = "";

    if (null == str) {

    return "";

    }

    try {

    result = java.net.URLEncoder.encode(str, ENCODE);

    } catch (UnsupportedEncodingException e) {

    e.printStackTrace();

    }

    return result;

    }

    /**

    * 正则替换

    * @param url

    * @param name

    * @param value

    * @return

    */

    public static String replaceUrlParameterReg(String url, String name, String value) {

    if (StringUtils.isNotBlank(url) && StringUtils.isNotBlank(value)) {

    url = url.replaceAll("(" + name + "=[^&]*)", name + "=" + value);

    }

    return url;

    }

    /**

    * 正则替换

    * @param url

    * @param name

    * @param value

    * @return

    */

    public static String getUrlParameterReg(String url, String name) {

    Map mapRequest = new HashMap();

    String[] arrSplit=null;

    String strUrlParam=TruncateUrlPage(url);

    if(strUrlParam==null)

    {

    return "";

    }

    //每个键值为一组 www.2cto.com

    arrSplit=strUrlParam.split("[&]");

    for(String strSplit:arrSplit)

    {

    String[] arrSplitEqual=null;

    arrSplitEqual= strSplit.split("[=]");

    //解析出键值

    if(arrSplitEqual.length>1)

    {

    //正确解析

    mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);

    }

    else

    {

    if(arrSplitEqual[0]!="")

    {

    //只有参数没有值,不加入

    mapRequest.put(arrSplitEqual[0], "");

    }

    }

    }

    return mapRequest.get(name);

    }

    /**

    * 去掉url中的路径,留下请求参数部分

    * @param strURL url地址

    * @return url请求参数部分

    */

    private static String TruncateUrlPage(String strURL)

    {

    String strAllParam=null;

    String[] arrSplit=null;

    strURL=strURL.trim().toLowerCase();

    arrSplit=strURL.split("[?]");

    if(strURL.length()>1)

    {

    if(arrSplit.length>1)

    {

    if(arrSplit[1]!=null)

    {

    strAllParam=arrSplit[1];

    }

    }

    }

    return strAllParam;

    }

    public static void main(String[] args) {

    String str ="/activePage?wxto=0C29975119DE037B4F25CD119CF4CF11&activity2018topid=oz9Zj07Jg5O9IfAyVauT-GL-CSfM&activity2018topunid=o-NUA0l8fxm2_5fmK_ocpyxrP1dE&subscribe=0&timestamp=1516008942920&-NUA0l8fxm2_5fmK_ocpyxrP1dE&oaccesstoken=5_xJYY3gnUTD4pqJFc7_ltbvclb0ZRKOFBEc5BXAwi7vjZndRAN58AkPazqUJDGzBbrw-J1ag1PF0C-S39SqJflA&m=1&wxt=C9A58DA3804E46981BF4B78C8C862743&-NUA0oHjJjCcyGe-wJdEFYjwrfU&-NUA0oHjJjCcyGe-wJdEFYjwrfU&-NUA0oHjJjCcyGe-wJdEFYjwrfU";

    String m = "9j70Dj%2BpVA64qK9%2FeOjsg2dP3biHh6imIC6WFXtIRcR6nHwDkjHPNOL9zUi7fmpcbgJP3u%2Bh3VUTfDO0B2fsbKWsizlqOKdXJovkg3HZMTxAIcByvYvtHoCyd9iOgUxoDqslzoMunvnEzPS8FbYc%2Fjkd4y47zhKk%2FUTE4GGm8ZM5Ns5eYwZSFL7A8pX0U%2BXfsMR%2FGf5z2pci6a5BJaki4e0z0Pmh5sPsjytmKdjdssHQ98iZ8ge3mXJm7G%2FaSJ3e%2FHvNENLv0JRJf7q1bFu180RZEtsMRs%2BWdesTuJamtJBYtLxQEYaCMygRcmd%2F1VdSqfLnuZqcZAzT6GAmvBwZyfzCK9LffGsyZGWce%2B0g2R%2FTCKTTtNpWWCJb0e7Gs0g82DmBNV4jh3hrOAmeLNwpu0M%2B049%2FHrDxAZPvXowCb5OLb7if%2BVqBpYkphGssurkqLV2KTXx3vhjl9vIiMQnD4397oNYf81P%2BBWFPBFhPQSfGaxZGZbAlPgkluR4%2Fj75f9SeZlcWFMnl%2BYKYtopw1gw%3D%3D";

    System.out.println(getURLEncoderString(m));

    System.out.println(getURLDecoderString(m));

    //System.out.println( getUrlParameterReg(str,"wxto"));

    }

    }

    展开全文
  • UrlEncode转码工具

    热门讨论 2012-05-19 21:41:22
    快速UrlEncode,UrlDecode;HtmlDecode,HtmlEncode相互转码。是web开发调试时必备工具
  • 转码工具

    2012-02-09 11:26:31
    有些字符在UE中无法正确转码 类似 8859-1 可以使用[url]http://kanjidict.stc.cx/recode.php[/url] Application Server f黵 EEA - Regi.on SAP Classic 转成 Application Server für EEA - Regi.on SAP ...
  • 解码 说明:本方法保证 不论参数s是否经过escape()编码,均能得到正确的“解码”结果。另外如果出现了Erro—Parameters: Character decoding failed. 请使用escape(escape(s)...解码URL中的中文,默认使用UTF-8进行解码
  • [url=http://www.movivi.com/]微视网[/url] [/1b]拥有非常强大的视频编解码、砖码技术,同时[1b] [url=http://www.movivi.com/]微视网[/url] [/1b]也在为广大网民提供Vlog业务。我试用了这款“V2V ...
  • 一般情况下,将带有中文的 url 拷贝到开发工具,开发工具都会有相应的转码(自动转码), 现在大部分的浏览器也可以对含有中文的 url 进行转码(自动转码) 情景说明 android 安卓系统,能够识别中文路径: ...
  • 转码

    2017-04-20 11:29:49
    我们开发中无论是传递网址还是在url后拼接中文,都需要转码,否则会乱码 转码用到的工具类和方法是: URLEncoder.encode(realName, "UTF-8");转码
  • 关于URL说起URL,大家第一反应...URL是一种强有力的工具。但URL并不完美。它表示的是实际的地址,而不是准确的名字。这种方案的缺点在于如果资源被移走了,URL也就不再有效了。那么它就无法对对象进行定位了 。UR...
  • 今天在使用Jsoup对一个网站进行数据爬取,发现爬取内容为该搜索结果是0条,请求头啥的都填好,利用Chrome开发者工具发现请求Url路径不含中文,抱着试一试的态度,我复制此段非中文参数进行请求,惊喜发现爬取结...
  • URL编码转换工具

    2007-11-20 10:40:13
    大家在IE地址中经常会看到url中一些特殊字符例如%20,%25其实就是空格和‘%’,此工具就是通过对字符的ASCII码转成十六进制的编码,方便大家开发时碰到的url特殊字符转码问题。
  • 发布此软件主要是看中搜索留痕转码URL的生成,内置了很多接口。 AN蜘蛛池华丽上线,软件内附蜘蛛池功能,(为各位新站长提供蜘蛛抓取网站)留痕转码,后期会上线跟多功能供各位使用。 最后郑重声明:软件禁止违法非法...
  • 探索网络API在开始使用网络API并与之交互时,我们的重点就像迄今为止所关注的那样—与设备商无关的工具和库。更具体地说,将研究cURL和Postman等工具,以用于基于HTTP的API;以及NETCONF over SSH,用于与网络设备上...
  • 关于这一次总结,中文转码问题 笔者刚碰到这个时候,也是一脸蒙蔽,因为使用的是 public String doHttpGet(String httpURL)throws IOException{ URLConnection connection = new URL(httpURL).openConnection()...
  • 最近用到Url编解码工具进行转码验证,网上找了一下,不是广告太多就是转换有问题,或者支持不够全面。根据实际经验总结了常用的几种编码转换形式:GBK、BIG5、UTF-8 BIG5特别说明下,其实BIG5是繁体中文编码的一种...
  • 从网上获取的 中文转码工具   package com.test; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; public class CharToolsUtil { public static final String Utf8...
  • 工具类包含了Emoji表情的转码和解码 包括了通过url获取文件大小的功能
  • 3.url编码与解码, 4.base64字符串的解码与转码, 5.base64图片的转码与解码, 6.简单poster工具的实现, 目前仅实现get,post方法, 支持头部信息 使用方法,头部采用 key:value形式,一行为一对参数, body参数为key=...
  • 视频下载与转码

    2018-09-17 09:33:24
    快速下载视频和转码   前提条件:安装了ffmpeg和you-get , 安装教程网上一大堆   下载视频可以使用you-get(一个开源项目) ...转码使用ffmpeg(也是一个开源项目,很强大的视频处理工具) 在命令行下运行:ffm...
  • 大家做爬虫时,是不是也遇到过url是中文,然后在显示时转码了的情况,一长串字符,根本就不知道是什么,要验证的时候很不方便,这里分享一下我的处理方法。一、尝试一长串字符,首先想到用在线unicode转中文,结果...
  • [url]http://simlee.iteye.com/blog/431611[/url] ...[参考]判断字符编码以及转码的一个工具类 http://hi.baidu.com/pazhu/blog/item/efcce7a2034ae9a8caefd05b.html 2008-07-01 08:55 /** * Date...
  • Eclipse文件转码插件

    2010-12-13 11:36:42
    转自([url]...真是个很不错的工具。 插件源代码下载 (导入插件在workspace中,右键export->Plug-in Development->Deployable plug-ins and fragments,然后生成插件binar文件) ...

空空如也

空空如也

1 2 3 4 5 ... 7
收藏数 123
精华内容 49
关键字:

url转码工具