URL增加encode和decode函数

git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@42 1a56cb94-b969-4eaa-88fa-be21384802f2
This commit is contained in:
william.liangf 2011-10-25 06:34:28 +00:00
parent fa1be21107
commit 3f34281f80
1 changed files with 27 additions and 15 deletions

View File

@ -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);
}
}
}