首页

关于dyuproject-web源码包中基于javax.servlet.http.HttpServletRequest实现的RequestUtil请求工具类获取参数getParams处理

标签:requestUtil,请求工具类,dyuproject     发布时间:2018-03-14   

一、前言

关于dyuproject-web源码包com.dyuproject.web.RequestUtil请求工具类,实现了javax.servlet.http.HttpServletRequest入参请求的解析转换为转为java.util.Map。

二、源码说明

package com.dyuproject.web;@b@@b@import com.dyuproject.util.Delim;@b@import java.io.IOException;@b@import java.util.Collections;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@import java.util.regex.Pattern;@b@import javax.servlet.http.HttpServletRequest;@b@import org.mortbay.util.MultiMap;@b@import org.mortbay.util.UrlEncoded;@b@@b@public final class RequestUtil@b@{@b@  public static final String URL_FORM_ENCODED = "application/x-www-form-urlencoded";@b@  public static final String PUT = "PUT";@b@  public static final String POST = "POST";@b@@b@  public static Map<String, String> getParams(HttpServletRequest request)@b@    throws IOException@b@  {@b@    String[] pairs;@b@    int i;@b@    Iterator iter;@b@    Map params = new HashMap();@b@@b@    String queryString = request.getQueryString();@b@    if ((queryString != null) && (queryString.length() > 0))@b@    {@b@      pairs = Delim.AMPER.split(queryString);@b@      for (i = 0; i < pairs.length; ++i)@b@      {@b@        String p = pairs[i];@b@        int idx = p.indexOf(61);@b@        params.put(p.substring(0, idx), p.substring(idx + 1));@b@      }@b@    }@b@    String contentType = request.getContentType();@b@    if ((contentType != null) && (contentType.length() > 0))@b@    {@b@      int idx = contentType.indexOf(59);@b@      contentType = (idx > 0) ? contentType.substring(0, idx).trim() : contentType.trim();@b@      if ((contentType.equalsIgnoreCase("application/x-www-form-urlencoded")) && ((("POST".equals(request.getMethod())) || ("PUT".equals(request.getMethod())))))@b@      {@b@        int length = request.getContentLength();@b@        if (length > 0)@b@        {@b@          MultiMap map = new MultiMap();@b@          UrlEncoded.decodeTo(request.getInputStream(), map, request.getCharacterEncoding(), -1);@b@          for (iter = map.toStringArrayMap().entrySet().iterator(); iter.hasNext(); )@b@          {@b@            Map.Entry entry = (Map.Entry)iter.next();@b@            String[] value = (String[])(String[])entry.getValue();@b@            params.put(((String)entry.getKey()).toString(), ((value != null) && (value.length > 0)) ? value[0] : null);@b@          }@b@        }@b@      }@b@    }@b@    return params;@b@  }@b@@b@  public static Map<String, String> parsePUTParams(HttpServletRequest request)@b@    throws IOException@b@  {@b@    if (!("PUT".equals(request.getMethod())))@b@      return Collections.emptyMap();@b@@b@    String contentType = request.getContentType();@b@    if ((contentType != null) && (contentType.length() > 0))@b@    {@b@      int idx = contentType.indexOf(59);@b@      contentType = (idx > 0) ? contentType.substring(0, idx).trim() : contentType.trim();@b@      int length = request.getContentLength();@b@      if (length > 0)@b@      {@b@        Map params = new HashMap();@b@        MultiMap map = new MultiMap();@b@        UrlEncoded.decodeTo(request.getInputStream(), map, request.getCharacterEncoding(), -1);@b@        for (Iterator iter = map.toStringArrayMap().entrySet().iterator(); iter.hasNext(); )@b@        {@b@          Map.Entry entry = (Map.Entry)iter.next();@b@          String[] value = (String[])(String[])entry.getValue();@b@          params.put(((String)entry.getKey()).toString(), ((value != null) && (value.length > 0)) ? value[0] : null);@b@        }@b@        return params;@b@      }@b@    }@b@    return Collections.emptyMap();@b@  }@b@}