当前位置: 代码迷 >> Web前端 >> request.getInputStream() 的两种解析模式
  详细解决方案

request.getInputStream() 的两种解析模式

热度:288   发布时间:2012-11-23 00:03:43.0
request.getInputStream() 的两种解析方式

request 内容解析方式:

方式一:

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

?方式二:

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;
	}
?
  相关解决方案