From c924d936f334b73cf16eb9fcdec350161c048217 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Fri, 13 Apr 2018 08:31:19 +0800 Subject: [PATCH] --- src/org/redkale/net/http/HttpRequest.java | 96 +++++++++++++++++++---- src/org/redkale/util/AnyValue.java | 70 ++++++++++++++--- 2 files changed, 140 insertions(+), 26 deletions(-) diff --git a/src/org/redkale/net/http/HttpRequest.java b/src/org/redkale/net/http/HttpRequest.java index 04b8f5f8e..855720c5c 100644 --- a/src/org/redkale/net/http/HttpRequest.java +++ b/src/org/redkale/net/http/HttpRequest.java @@ -632,7 +632,11 @@ public class HttpRequest extends Request { public short getRequstURILastPath(short defvalue) { String val = getRequstURILastPath(); if (val.isEmpty()) return defvalue; - return val.isEmpty() ? defvalue : Short.parseShort(val); + try { + return Short.parseShort(val); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -648,7 +652,11 @@ public class HttpRequest extends Request { public short getRequstURILastPath(int radix, short defvalue) { String val = getRequstURILastPath(); if (val.isEmpty()) return defvalue; - return val.isEmpty() ? defvalue : Short.parseShort(val, radix); + try { + return Short.parseShort(val, radix); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -662,7 +670,11 @@ public class HttpRequest extends Request { */ public int getRequstURILastPath(int defvalue) { String val = getRequstURILastPath(); - return val.isEmpty() ? defvalue : Integer.parseInt(val); + try { + return val.isEmpty() ? defvalue : Integer.parseInt(val); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -677,7 +689,11 @@ public class HttpRequest extends Request { */ public int getRequstURILastPath(int radix, int defvalue) { String val = getRequstURILastPath(); - return val.isEmpty() ? defvalue : Integer.parseInt(val, radix); + try { + return val.isEmpty() ? defvalue : Integer.parseInt(val, radix); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -691,7 +707,11 @@ public class HttpRequest extends Request { */ public float getRequstURILastPath(float defvalue) { String val = getRequstURILastPath(); - return val.isEmpty() ? defvalue : Float.parseFloat(val); + try { + return val.isEmpty() ? defvalue : Float.parseFloat(val); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -705,7 +725,11 @@ public class HttpRequest extends Request { */ public long getRequstURILastPath(long defvalue) { String val = getRequstURILastPath(); - return val.isEmpty() ? defvalue : Long.parseLong(val); + try { + return val.isEmpty() ? defvalue : Long.parseLong(val); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -720,7 +744,11 @@ public class HttpRequest extends Request { */ public long getRequstURILastPath(int radix, long defvalue) { String val = getRequstURILastPath(); - return val.isEmpty() ? defvalue : Long.parseLong(val, radix); + try { + return val.isEmpty() ? defvalue : Long.parseLong(val, radix); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -734,7 +762,11 @@ public class HttpRequest extends Request { */ public double getRequstURILastPath(double defvalue) { String val = getRequstURILastPath(); - return val.isEmpty() ? defvalue : Double.parseDouble(val); + try { + return val.isEmpty() ? defvalue : Double.parseDouble(val); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -781,7 +813,11 @@ public class HttpRequest extends Request { */ public short getRequstURIPath(String prefix, short defvalue) { String val = getRequstURIPath(prefix, null); - return val == null ? defvalue : Short.parseShort(val); + try { + return val == null ? defvalue : Short.parseShort(val); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -797,7 +833,11 @@ public class HttpRequest extends Request { */ public short getRequstURIPath(int radix, String prefix, short defvalue) { String val = getRequstURIPath(prefix, null); - return val == null ? defvalue : Short.parseShort(val, radix); + try { + return val == null ? defvalue : Short.parseShort(val, radix); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -813,7 +853,11 @@ public class HttpRequest extends Request { */ public int getRequstURIPath(String prefix, int defvalue) { String val = getRequstURIPath(prefix, null); - return val == null ? defvalue : Integer.parseInt(val); + try { + return val == null ? defvalue : Integer.parseInt(val); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -830,7 +874,11 @@ public class HttpRequest extends Request { */ public int getRequstURIPath(int radix, String prefix, int defvalue) { String val = getRequstURIPath(prefix, null); - return val == null ? defvalue : Integer.parseInt(val, radix); + try { + return val == null ? defvalue : Integer.parseInt(val, radix); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -845,7 +893,11 @@ public class HttpRequest extends Request { */ public float getRequstURIPath(String prefix, float defvalue) { String val = getRequstURIPath(prefix, null); - return val == null ? defvalue : Float.parseFloat(val); + try { + return val == null ? defvalue : Float.parseFloat(val); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -860,7 +912,11 @@ public class HttpRequest extends Request { */ public long getRequstURIPath(String prefix, long defvalue) { String val = getRequstURIPath(prefix, null); - return val == null ? defvalue : Long.parseLong(val); + try { + return val == null ? defvalue : Long.parseLong(val); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -876,7 +932,11 @@ public class HttpRequest extends Request { */ public long getRequstURIPath(int radix, String prefix, long defvalue) { String val = getRequstURIPath(prefix, null); - return val == null ? defvalue : Long.parseLong(val, radix); + try { + return val == null ? defvalue : Long.parseLong(val, radix); + } catch (NumberFormatException e) { + return defvalue; + } } /** @@ -891,7 +951,11 @@ public class HttpRequest extends Request { */ public double getRequstURIPath(String prefix, double defvalue) { String val = getRequstURIPath(prefix, null); - return val == null ? defvalue : Double.parseDouble(val); + try { + return val == null ? defvalue : Double.parseDouble(val); + } catch (NumberFormatException e) { + return defvalue; + } } //------------------------------------------------------------------------------ diff --git a/src/org/redkale/util/AnyValue.java b/src/org/redkale/util/AnyValue.java index 5bfd061aa..7913e8f56 100644 --- a/src/org/redkale/util/AnyValue.java +++ b/src/org/redkale/util/AnyValue.java @@ -487,12 +487,22 @@ public abstract class AnyValue { public byte getByteValue(String name, byte defaultValue) { String value = getValue(name); - return value == null || value.length() == 0 ? defaultValue : Byte.decode(value); + if (value == null || value.length() == 0) return defaultValue; + try { + return Byte.decode(value); + } catch (NumberFormatException e) { + return defaultValue; + } } public byte getByteValue(int radix, String name, byte defaultValue) { String value = getValue(name); - return value == null || value.length() == 0 ? defaultValue : (radix == 10 ? Byte.decode(value) : Byte.parseByte(value, radix)); + if (value == null || value.length() == 0) return defaultValue; + try { + return (radix == 10 ? Byte.decode(value) : Byte.parseByte(value, radix)); + } catch (NumberFormatException e) { + return defaultValue; + } } public char getCharValue(String name) { @@ -510,12 +520,22 @@ public abstract class AnyValue { public short getShortValue(String name, short defaultValue) { String value = getValue(name); - return value == null || value.length() == 0 ? defaultValue : Short.decode(value); + if (value == null || value.length() == 0) return defaultValue; + try { + return Short.decode(value); + } catch (NumberFormatException e) { + return defaultValue; + } } public short getShortValue(int radix, String name, short defaultValue) { String value = getValue(name); - return value == null || value.length() == 0 ? defaultValue : (radix == 10 ? Short.decode(value) : Short.parseShort(value, radix)); + if (value == null || value.length() == 0) return defaultValue; + try { + return (radix == 10 ? Short.decode(value) : Short.parseShort(value, radix)); + } catch (NumberFormatException e) { + return defaultValue; + } } public int getIntValue(String name) { @@ -524,12 +544,22 @@ public abstract class AnyValue { public int getIntValue(String name, int defaultValue) { String value = getValue(name); - return value == null || value.length() == 0 ? defaultValue : Integer.decode(value); + if (value == null || value.length() == 0) return defaultValue; + try { + return Integer.decode(value); + } catch (NumberFormatException e) { + return defaultValue; + } } public int getIntValue(int radix, String name, int defaultValue) { String value = getValue(name); - return value == null || value.length() == 0 ? defaultValue : (radix == 10 ? Integer.decode(value) : Integer.parseInt(value, radix)); + if (value == null || value.length() == 0) return defaultValue; + try { + return (radix == 10 ? Integer.decode(value) : Integer.parseInt(value, radix)); + } catch (NumberFormatException e) { + return defaultValue; + } } public long getLongValue(String name) { @@ -538,12 +568,22 @@ public abstract class AnyValue { public long getLongValue(String name, long defaultValue) { String value = getValue(name); - return value == null || value.length() == 0 ? defaultValue : Long.decode(value); + if (value == null || value.length() == 0) return defaultValue; + try { + return Long.decode(value); + } catch (NumberFormatException e) { + return defaultValue; + } } public long getLongValue(int radix, String name, long defaultValue) { String value = getValue(name); - return value == null || value.length() == 0 ? defaultValue : (radix == 10 ? Long.decode(value) : Long.parseLong(value, radix)); + if (value == null || value.length() == 0) return defaultValue; + try { + return (radix == 10 ? Long.decode(value) : Long.parseLong(value, radix)); + } catch (NumberFormatException e) { + return defaultValue; + } } public float getFloatValue(String name) { @@ -552,7 +592,12 @@ public abstract class AnyValue { public float getFloatValue(String name, float defaultValue) { String value = getValue(name); - return value == null || value.length() == 0 ? defaultValue : Float.parseFloat(value); + if (value == null || value.length() == 0) return defaultValue; + try { + return Float.parseFloat(value); + } catch (NumberFormatException e) { + return defaultValue; + } } public double getDoubleValue(String name) { @@ -561,7 +606,12 @@ public abstract class AnyValue { public double getDoubleValue(String name, double defaultValue) { String value = getValue(name); - return value == null || value.length() == 0 ? defaultValue : Double.parseDouble(value); + if (value == null || value.length() == 0) return defaultValue; + try { + return Double.parseDouble(value); + } catch (NumberFormatException e) { + return defaultValue; + } } public String getValue(String name, String defaultValue) {