From 3f34281f805bd47b9d4af03de5357fe52a2240c2 Mon Sep 17 00:00:00 2001 From: "william.liangf" Date: Tue, 25 Oct 2011 06:34:28 +0000 Subject: [PATCH] =?UTF-8?q?URL=E5=A2=9E=E5=8A=A0encode=E5=92=8Cdecode?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@42 1a56cb94-b969-4eaa-88fa-be21384802f2 --- .../java/com/alibaba/dubbo/common/URL.java | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/URL.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/URL.java index ad5c13bef..aa642f94c 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/URL.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/URL.java @@ -218,13 +218,10 @@ public final class URL implements Serializable { } public URL addParameterAndEncoded(String key, String value) { - if(value == null || value.length() == 0) return this; - try { - value = URLEncoder.encode(value, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e.getMessage(), e); + if(value == null || value.length() == 0) { + return this; } - return addParameter(key, value); + return addParameter(key, encode(value)); } public URL addParameter(String key, boolean value) { @@ -591,15 +588,7 @@ public final class URL implements Serializable { } public String getParameterAndDecoded(String key, String defaultValue) { - String value = getParameter(key, defaultValue); - if (value != null && value.length() > 0) { - try { - value = URLDecoder.decode(value, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e.getMessage(), e); - } - } - return value; + return decode(getParameter(key, defaultValue)); } public String getParameter(String key) { @@ -794,4 +783,27 @@ public final class URL implements Serializable { String value = getMethodParameter(method, key); return value != null && value.length() > 0; } + + public static String encode(String value) { + if (value == null || value.length() == 0) { + return ""; + } + try { + return URLEncoder.encode(value, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e.getMessage(), e); + } + } + + public static String decode(String value) { + if (value == null || value.length() == 0) { + return ""; + } + try { + return URLDecoder.decode(value, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e.getMessage(), e); + } + } + } \ No newline at end of file -- GitLab