request.getInputStream()の2つの解析方式

1530 ワード

requestコンテンツ解析方式:
方法1:
byte[] bytes = new byte[1024 * 1024];
			InputStream is = request.getInputStream();

			int nRead = 1;
			int nTotalRead = 0;
			while (nRead > 0) {
				nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);
				if (nRead > 0)
					nTotalRead = nTotalRead + nRead;
			}
			String str = new String(bytes, 0, nTotalRead, "utf-8");
			System.out.println("Str:" + str);

方式2:
BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream()));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			System.out.println(sb);
 
ヘッダパラメータ解析方式
 
String params = request.getQueryString();

private Map getRequestParam(String params){
		Map paramMap = null;
		if(null!=params){
			paramMap = new HashMap();
			String param[]=params.split("&");
     	   for(int i=0;i<param.length;i++){
     		   String content = param[i];
     		   String key = content.substring(0, content.indexOf("="));
     		   String value = content.substring(content.indexOf("=")+1, content.length());
     		  paramMap.put(key, value);
     	   }
		}
		return paramMap;
	}