URI参数编码 @param uri @return
(String uri)
| 462 | * @return |
| 463 | */ |
| 464 | public static String parameterEncoded(String uri){ |
| 465 | //截取到等于第二个参数的字符串为止,从左往右 |
| 466 | String uri_before = StringUtils.substringBefore(uri, "?"); |
| 467 | |
| 468 | //从左往右查到相等的字符开始,保留后边的,不包含等于的字符 |
| 469 | String queryString = StringUtils.substringAfter(uri, "?"); |
| 470 | |
| 471 | //组装URL参数 |
| 472 | StringBuffer url_parameter = new StringBuffer(""); |
| 473 | //参数进行URL编码 |
| 474 | if(queryString != null && !queryString.trim().isEmpty()){ |
| 475 | MultiMap<String> values = new MultiMap<String>(); |
| 476 | UrlEncoded.decodeTo(queryString, values, StandardCharsets.UTF_8); |
| 477 | Iterator iter = values.entrySet().iterator(); |
| 478 | while(iter.hasNext()){ |
| 479 | Map.Entry e = (Map.Entry)iter.next(); |
| 480 | if(e.getValue() != null){ |
| 481 | if(e.getValue() instanceof List){ |
| 482 | List<String> valueList = (List)e.getValue(); |
| 483 | if(valueList.size() >0){ |
| 484 | for(String value :valueList){ |
| 485 | if(value != null && !value.trim().isEmpty()){ |
| 486 | |
| 487 | url_parameter.append("&"+e.getKey()+"="+URLEncoder.encode(value, StandardCharsets.UTF_8)); |
| 488 | |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | } |
| 493 | }else{ |
| 494 | if(e.getValue() instanceof String){ |
| 495 | String value = e.getValue().toString().trim(); |
| 496 | if(!value.isEmpty()){ |
| 497 | url_parameter.append("&"+e.getKey()+"="+URLEncoder.encode(value, StandardCharsets.UTF_8)); |
| 498 | |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | |
| 505 | |
| 506 | } |
| 507 | } |
| 508 | String new_url_parameter = ""; |
| 509 | if(url_parameter.length() >1){ |
| 510 | new_url_parameter = "?"; |
| 511 | //删除第一个& |
| 512 | new_url_parameter += StringUtils.difference("&", url_parameter.toString()); |
| 513 | } |
| 514 | return uri_before+new_url_parameter; |
| 515 | |
| 516 | } |
| 517 | |
| 518 | |
| 519 | /** |