消失的昨天 發表於 2025-6-14 21:30:00

SpringBoot进阶教程(八十六)URL指定参数encode

<div class="bodyCustomClass">
<div class="bodyCustomClass">
<blockquote>
<p>Encode(编码)和Decode(解码)是在信息处理中常用的概念,用于表示将信息从一种形式转换为另一种形式的过程。</p>
</blockquote>
<h2 id="_nav_0" class="blogCustomTitleStyle"><span class="blogCustomTitleIco">v</span>直接demo</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.UnsupportedEncodingException;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.net.URLDecoder;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.net.URLEncoder;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.ArrayList;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> UrlParamEncoder {

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> UnsupportedEncodingException {
      String url </span>= "https://www.test.com/s?wd=%E7%99%BE%E5%BA%A6&amp;rsv_spt=1&amp;rsv_iqid=0xbe352d6c00064d34&amp;issp=1&amp;f=8&amp;rsv_bp=1&amp;rsv_idx=2&amp;ie=utf-8&amp;tn=baiduhome_pg&amp;rsv_dl=tb&amp;rsv_enter=1&amp;rsv_sug3=17&amp;rsv_sug1=8&amp;rsv_sug7=100&amp;rsv_sug2=0&amp;rsv_btype=i&amp;prefixsug=%25E7%2599%25BE%25E5%25BA%25A6&amp;rsp=6&amp;inputT=3412&amp;rsv_sug4=5053"<span style="color: rgba(0, 0, 0, 1)">;
      String encodedUrl </span>= encodeSpecificParam(url, "prefixsug", <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
      System.out.println(url);
      System.out.println(encodedUrl);
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 对指定参数进行编码
   * ignore是否先解码再编码
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> url
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> targetParam
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> ignore
   * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
   <span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> String encodeSpecificParam(String url, String targetParam, <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> ignore) {
      </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 分割基础URL和查询部分</span>
            String[] urlParts = url.split("\\?", 2<span style="color: rgba(0, 0, 0, 1)">);
            String baseUrl </span>= urlParts;
            String queryAndFragment </span>= urlParts.length &gt; 1 ? urlParts : ""<span style="color: rgba(0, 0, 0, 1)">;

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 分离查询参数和锚点</span>
            String[] queryFragment = queryAndFragment.split("#", 2<span style="color: rgba(0, 0, 0, 1)">);
            String query </span>= queryFragment;
            String fragment </span>= queryFragment.length &gt; 1 ? "#" + queryFragment : ""<span style="color: rgba(0, 0, 0, 1)">;

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 解析查询参数</span>
            List&lt;Param&gt; params =<span style="color: rgba(0, 0, 0, 1)"> parseQueryString(query);

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 编码目标参数</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Param param : params) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (param.key.equals(targetParam)) {
                  String decoded </span>= ignore ? param.value : URLDecoder.decode(param.value, "UTF-8"<span style="color: rgba(0, 0, 0, 1)">);
                  String encoded </span>= URLEncoder.encode(decoded, "UTF-8"<span style="color: rgba(0, 0, 0, 1)">)
                            .replace(</span>"+", "%20"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将+转换为%20</span>
                  param.value =<span style="color: rgba(0, 0, 0, 1)"> encoded;
                }
            }

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 重构查询字符串</span>
            StringBuilder newQuery = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuilder();
            </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Param param : params) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (newQuery.length() &gt; 0<span style="color: rgba(0, 0, 0, 1)">) {
                  newQuery.append(</span>"&amp;"<span style="color: rgba(0, 0, 0, 1)">);
                }
                newQuery.append(param.key).append(</span>"="<span style="color: rgba(0, 0, 0, 1)">).append(param.value);
            }

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 组合最终URL</span>
            <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> baseUrl
                  </span>+ (newQuery.length() &gt; 0 ? "?" : ""<span style="color: rgba(0, 0, 0, 1)">)
                  </span>+<span style="color: rgba(0, 0, 0, 1)"> newQuery.toString()
                  </span>+<span style="color: rgba(0, 0, 0, 1)"> fragment;

      } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (UnsupportedEncodingException e) {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> RuntimeException("字符编码错误"<span style="color: rgba(0, 0, 0, 1)">, e);
      }
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> List&lt;Param&gt;<span style="color: rgba(0, 0, 0, 1)"> parseQueryString(String query) {
      List</span>&lt;Param&gt; params = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (query == <span style="color: rgba(0, 0, 255, 1)">null</span> || query.isEmpty()) <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> params;

      </span><span style="color: rgba(0, 0, 255, 1)">for</span> (String pair : query.split("&amp;"<span style="color: rgba(0, 0, 0, 1)">)) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> idx = pair.indexOf("="<span style="color: rgba(0, 0, 0, 1)">);
            String key </span>= (idx &gt; 0) ? pair.substring(0<span style="color: rgba(0, 0, 0, 1)">, idx) : pair;
            String value </span>= (idx &gt; 0 &amp;&amp; pair.length() &gt; idx + 1) ? pair.substring(idx + 1) : ""<span style="color: rgba(0, 0, 0, 1)">;
            params.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Param(key, value));
      }
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> params;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Param {
      String key;
      String value;

      Param(String key, String value) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.key =<span style="color: rgba(0, 0, 0, 1)"> key;
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.value =<span style="color: rgba(0, 0, 0, 1)"> value;
      }
    }
}</span></pre>
</div>
<h2 id="_nav_99" class="blogCustomTitleStyle"><span class="blogCustomTitleIco">v</span>源码地址</h2>
<p>https://github.com/toutouge/javademosecond/tree/master/hellolearn</p>
<div id="MySignature" style="display: block">
<p id="PSignature" style="padding: 10px 10px 10px 60px; background: url(&quot;http://images.cnblogs.com/cnblogs_com/toutou/682006/o_122329534672560.png&quot;) 1% no-repeat rgba(229, 241, 244, 1); font-family: 微软雅黑; font-size: 12px; border: 1px dashed rgba(224, 224, 224, 1)"> <br>
                作  者:<strong><span style="font-size: 12px; color: rgba(255, 0, 0, 1)">请叫我头头哥</span></strong>
                <br>
                出  处:http://www.cnblogs.com/toutou/
                <br>
                关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
                <br>
                版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
                <br>
                特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
                <br>
                声援博主:如果您觉得文章对您有帮助,可以点击文章右下角<strong><span style="color: rgba(255, 0, 0, 1); font-size: 18pt">【推荐】</span></strong>一下。您的鼓励是作者坚持原创和持续写作的最大动力!
                <br>
      </p>



</div>
</div>
</div>
<style>#comment_body_3242240 { display: none }</style><br><br>
来源:https://www.cnblogs.com/toutou/p/18596841/springboot_url_param_encode
頁: [1]
查看完整版本: SpringBoot进阶教程(八十六)URL指定参数encode