apache.catalina, tomcat 的http请求,queryString 请求参数解析

可以使用 tomcat的org.apache.catalina.util.RequestUtil 类,方法如下

public static void main(String[] args) {
		String query="name=tom&age=ee&";

		Map<String, String[]> values = new HashMap<>();
		try {
		    RequestUtil.parseParameters(values, query, "utf8");
		    for (Map.Entry<String, String[]> entry : values.entrySet()) {
		        System.out.println(entry.getKey() + ":" + entry.getValue()[0] + ";");
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		}
	}

目前tomcat-6x 版本下的此类没有依赖,源码如下



import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.TimeZone;

public final class RequestUtil {

   private static SimpleDateFormat format = new SimpleDateFormat(" EEEE, dd-MMM-yy kk:mm:ss zz");
   public static String filter(String message) {
      if(message == null) {
         return null;
      } else {
         char[] content = new char[message.length()];
         message.getChars(0, message.length(), content, 0);
         StringBuffer result = new StringBuffer(content.length + 50);

         for(int i = 0; i < content.length; ++i) {
            switch(content[i]) {
            case 34:
               result.append(""");
               break;
            case 38:
               result.append("&");
               break;
            case 60:
               result.append("<");
               break;
            case 62:
               result.append(">");
               break;
            default:
               result.append(content[i]);
            }
         }

         return result.toString();
      }
   }

   public static String normalize(String path) {
      return normalize(path, true);
   }

   public static String normalize(String path, boolean replaceBackSlash) {
      if(path == null) {
         return null;
      } else {
         String normalized = path;
         if(replaceBackSlash && path.indexOf(92) >= 0) {
            normalized = path.replace('\\', '/');
         }

         if(!normalized.startsWith("/")) {
            normalized = "/" + normalized;
         }

         while(true) {
            int index = normalized.indexOf("//");
            if(index < 0) {
               while(true) {
                  index = normalized.indexOf("/./");
                  if(index < 0) {
                     while(true) {
                        index = normalized.indexOf("/../");
                        if(index < 0) {
                           return normalized.equals("/.")?"/":(normalized.equals("/..")?null:normalized);
                        }

                        if(index == 0) {
                           return null;
                        }

                        int index2 = normalized.lastIndexOf(47, index - 1);
                        normalized = normalized.substring(0, index2) + normalized.substring(index + 3);
                     }
                  }

                  normalized = normalized.substring(0, index) + normalized.substring(index + 2);
               }
            }

            normalized = normalized.substring(0, index) + normalized.substring(index + 1);
         }
      }
   }

   public static void parseParameters(Map map, String data, String encoding) throws UnsupportedEncodingException {
      if(data != null && data.length() > 0) {
         byte[] bytes = null;

         try {
            if(encoding == null) {
               bytes = data.getBytes();
            } else {
               bytes = data.getBytes(encoding);
            }
         } catch (UnsupportedEncodingException var5) {
            ;
         }

         parseParameters(map, (byte[])bytes, encoding);
      }

   }

   public static String URLDecode(String str) {
      return URLDecode((String)str, (String)null);
   }

   public static String URLDecode(String str, String enc) {
      return URLDecode((String)str, enc, false);
   }

   public static String URLDecode(String str, String enc, boolean isQuery) {
      if(str == null) {
         return null;
      } else {
         byte[] bytes = null;

         try {
            if(enc == null) {
               bytes = str.getBytes();
            } else {
               bytes = str.getBytes(enc);
            }
         } catch (UnsupportedEncodingException var5) {
            ;
         }

         return URLDecode((byte[])bytes, enc, isQuery);
      }
   }

   public static String URLDecode(byte[] bytes) {
      return URLDecode((byte[])bytes, (String)null);
   }

   public static String URLDecode(byte[] bytes, String enc) {
      return URLDecode((byte[])bytes, enc, false);
   }

   public static String URLDecode(byte[] bytes, String enc, boolean isQuery) {
      if(bytes == null) {
         return null;
      } else {
         int len = bytes.length;
         int ix = 0;

         int ox;
         byte e;
         for(ox = 0; ix < len; bytes[ox++] = e) {
            e = bytes[ix++];
            if(e == 43 && isQuery) {
               e = 32;
            } else if(e == 37) {
               e = (byte)((convertHexDigit(bytes[ix++]) << 4) + convertHexDigit(bytes[ix++]));
            }
         }

         if(enc != null) {
            try {
               return new String(bytes, 0, ox, enc);
            } catch (Exception var7) {
               var7.printStackTrace();
            }
         }

         return new String(bytes, 0, ox);
      }
   }

   private static byte convertHexDigit(byte b) {
      return b >= 48 && b <= 57?(byte)(b - 48):(b >= 97 && b <= 102?(byte)(b - 97 + 10):(b >= 65 && b <= 70?(byte)(b - 65 + 10):0));
   }

   private static void putMapEntry(Map map, String name, String value) {
      String[] newValues = null;
      String[] oldValues = (String[])((String[])map.get(name));
      if(oldValues == null) {
         newValues = new String[]{value};
      } else {
         newValues = new String[oldValues.length + 1];
         System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
         newValues[oldValues.length] = value;
      }

      map.put(name, newValues);
   }

   public static void parseParameters(Map map, byte[] data, String encoding) throws UnsupportedEncodingException {
      if(data != null && data.length > 0) {
         int ix = 0;
         int ox = 0;
         String key = null;
         String value = null;

         while(ix < data.length) {
            byte c = data[ix++];
            switch((char)c) {
            case 37:
               data[ox++] = (byte)((convertHexDigit(data[ix++]) << 4) + convertHexDigit(data[ix++]));
               break;
            case 38:
               value = new String(data, 0, ox, encoding);
               if(key != null) {
                  putMapEntry(map, key, value);
                  key = null;
               }

               ox = 0;
               break;
            case 43:
               data[ox++] = 32;
               break;
            case 61:
               if(key == null) {
                  key = new String(data, 0, ox, encoding);
                  ox = 0;
               } else {
                  data[ox++] = c;
               }
               break;
            default:
               data[ox++] = c;
            }
         }

         if(key != null) {
            value = new String(data, 0, ox, encoding);
            putMapEntry(map, key, value);
         }
      }

   }

   static {
      format.setTimeZone(TimeZone.getTimeZone("GMT"));
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

life1024

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值