java如何实现python的urllib.quote(str,safe='')

java如何实现python的urllib.quote(str,safe=''),第1张

概述最近需要将一些python代码转成java,遇到url编码 urllib.quote(str,safe='/') 但java中URLEncoder.encode(arg, Consta

最近需要将一些python代码转成java,遇到url编码

urllib.quote(str,safe='/')

但java中URLEncoder.encode(arg,Constant.UTF_8)会将'/'转成%2F

网上查了一下 java没见到类似的safe方式,只好自己实现一个类

<span >public <span >class<span > UrlSafeEncoder {

</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;static</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; BitSet dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;final</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span> caseDiff = ('a' - 'A'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;static</span> String dfltEnc<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;static</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; {    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; The <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a> of ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters that are not encoded has been     * deter<a href="https://www.jb51.cc/tag/mine/" target="_blank" >mine</a>d as f<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>lows:     *     * RFC 2396 states:     * -----     * Data ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters 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 <a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>mited set of     * punctuation marks and symb<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>s.     *     * unreserved  = <a href="https://m.jb51.cc/tag/Alpha/" target="_blank" >Alpha</a>num | mark     *     * mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"     *     * Unreserved ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters can be escaped without changing the     * semantics of the URI,but this sho<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>d not be done unless the     * URI is being used in a context that does not allow the     * u<a href="https://www.jb51.cc/tag/nes/" target="_blank" >nes</a>caped ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter to appear.     * -----     *     * It appears that both <a href="https://www.jb51.cc/tag/netscape/" target="_blank" >netscape</a> and Internet Explorer escape     * all special ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters from this <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a> with the exception     * of "-","_",".","*". While it is not clear why t<a href="https://www.jb51.cc/tag/hey/" target="_blank" >hey</a> are     * esca<a href="https://m.jb51.cc/tag/Ping/" target="_blank" >Ping</a> the other ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters,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     * <a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>. It is also noteworthy that this is consistent with     * O'Reilly's "<a href="https://m.jb51.cc/tag/HTML/" target="_blank" >HTML</a>: The Definitive Gu<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>e" (page 164).     *     * As a last note,Intenet Explorer does not encode the "@"     * ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter which is clearly not unreserved according to the     * RFC. We are being consistent with the RFC in this matter,* as is <a href="https://www.jb51.cc/tag/netscape/" target="_blank" >netscape</a>.     *     </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;    dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;new</span> BitSet(256<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; i;    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;for</span> (i = 'a'; i <= 'z'; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) {        dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(i);    }    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;for</span> (i = 'A'; i <= 'Z'; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) {        dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(i);    }    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;for</span> (i = '0'; i <= '9'; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) {        dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(i);    }    dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(</span>' '); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> a space to a + is done                                * in the encode() method </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;    dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(</span>'-'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);    dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(</span>'_'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);    dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(</span>'.'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);    dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(</span>'*'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);    dfltEnc<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; AccessContr<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>ler.doPrivileged(        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;new</span> GetPropertyAction("<a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>.en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;)    );}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;/**</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; * You can't call the constructor. </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;private</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; UrlSafeEncoder() { }</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;/**</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; * Translates a string into {</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"&gt;@code</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; app<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>cation/x-www-form-urlencoded} * format using a specific en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> scheme. This method uses the * suppl<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>d en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> scheme to obtain the bytes for unsafe * ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters. * <p> * <em><strong>Note:</strong> The <a href= * "</span><span &gt;http://www.w3.org/TR/html40/appendix/notes.html</span><span &gt;#non-ascii-chars"&gt; * World Wide Web Consortium Recommendation</a> states that * UTF-8 sho<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>d be used. Not doing so may introduce * incompatibi<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>t<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>s.</em> * * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"&gt;@p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;   s   {</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"&gt;@code</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; String} to be translated. * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"&gt;@p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;   enc   The <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> of a supported *    <a <a href="https://m.jb51.cc/tag/href/" target="_blank" >href</a>="../lang/package-summary.<a href="https://m.jb51.cc/tag/HTML/" target="_blank" >HTML</a>#charenc"&gt;ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter *    en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a></a>. * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"&gt;@return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;  the translated {</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"&gt;@code</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; String}. * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"&gt;@exception</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;  UnsupportedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>Exception *             If the <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>d en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> is not supported * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"&gt;@see</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; URLDecoder#decode(<a href="https://www.jb51.cc/tag/javalang/" target="_blank" >java.lang</a>.String,<a href="https://www.jb51.cc/tag/javalang/" target="_blank" >java.lang</a>.String) * </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"&gt;@since</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 1.4 </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;static</span> String encode(String s,String enc,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;char</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; safe)    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;throws</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; UnsupportedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>Exception {    dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.set(safe);    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;bo<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>ean</span> need<a href="https://www.jb51.cc/tag/toch/" target="_blank" >toch</a>ange = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;false</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;;    StringBuffer out </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; StringBuffer(s.length());    Charset charset;    Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter();    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span> (enc == <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;n<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>l</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;)        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;throw</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;new</span> N<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>lPointerException("charset<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;try</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; {        charset </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; Charset.for<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>(enc);    } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; (Ille<a href="https://www.jb51.cc/tag/gal/" target="_blank" >gal</a>Charset<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>Exception e) {        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;throw</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; UnsupportedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>Exception(enc);    } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; (UnsupportedCharsetException e) {        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;throw</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; UnsupportedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>Exception(enc);    }    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span> i = 0; i <<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; s.length();) {        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span> c = (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) s.ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>t(i);        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println("Examining ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter: " + c);</span>        <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; (dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.get(c)) {            </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span> (c == ' '<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) {                c </span>= '+'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;;                need<a href="https://www.jb51.cc/tag/toch/" target="_blank" >toch</a>ange </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;;            }            </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println("Storing: " + c);</span>            out.append((<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;char</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;)c);            i</span>++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;;        } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; {            </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; convert to external en<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a> before hex conversion</span>            <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;do</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; {                ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter.write(c);                </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;                 * If this ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter represents the start of a Unicode                 * surrogate pair,then pass in two ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cters. It's not                 * clear what sho<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>d be done if a bytes reserved in the                 * surrogate pa<a href="https://www.jb51.cc/tag/irs/" target="_blank" >irs</a> range occurs outs<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>e of a le<a href="https://www.jb51.cc/tag/gal/" target="_blank" >gal</a>                 * surrogate pair. For <a href="https://www.jb51.cc/tag/Now/" target="_blank" >Now</a>,just treat it as if it were                 * any other ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter.                 </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;*/</span>                <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span> (c >= 0xD800 &amp;&amp; c <= 0xDBFF<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) {                    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;                      Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println(Integer.toHexString(c)                      + " is high surrogate");                    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;*/</span>                    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span> ( (i+1) <<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; s.length()) {                        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span> d = (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span>) s.ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>t(i+1<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);                        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;                          Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println("\tExamining "                          + Integer.toHexString(d));                        </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;*/</span>                        <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span> (d >= 0xDC00 &amp;&amp; d <= 0xDFFF<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) {                            </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;                              Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.out.println("\t"                              + Integer.toHexString(d)                              + " is low surrogate");                            </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;                            ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter.write(d);                            i</span>++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;;                        }                    }                }                i</span>++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;;            } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;while</span> (i < s.length() &amp;&amp; !dontNeedEn<a href="https://m.jb51.cc/tag/Coding/" target="_blank" >Coding</a>.get((c = (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) s.ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>t(i))));            ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter.flush();            String str </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; String(ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter.<a href="https://www.jb51.cc/tag/toch/" target="_blank" >toch</a><a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rray());            </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;byte</span>[] ba =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; str.getBytes(charset);            </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span> j = 0; j < ba.length; j++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;) {                out.append(</span>'%'<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);                </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;char</span> ch = Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter.forDigit((ba[j] >> 4) &amp; 0xF,16<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);                </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; converting to use uppercase letter as part of                </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; the hex value if ch is a letter.</span>                <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; (Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter.isLetter(ch)) {                    ch </span>-=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; caseDiff;                }                out.append(ch);                ch </span>= Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter.forDigit(ba[j] &amp; 0xF,16<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);                </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; (Ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>cter.isLetter(ch)) {                    ch </span>-=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; caseDiff;                }                out.append(ch);            }            ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>rrayWriter.<a href="https://m.jb51.cc/tag/reset/" target="_blank" >reset</a>();            need<a href="https://www.jb51.cc/tag/toch/" target="_blank" >toch</a>ange </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;;        }    }    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;return</span> (need<a href="https://www.jb51.cc/tag/toch/" target="_blank" >toch</a>ange?<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; out.toString() : s);}

}

验证下 基本ok

总结

以上是内存溢出为你收集整理的java如何实现python的urllib.quote(str,safe='/')全部内容,希望文章能够帮你解决java如何实现python的urllib.quote(str,safe='/')所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1208068.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存