关闭

java Servlet 接收POST数据格式

1664人阅读 评论(0) 收藏 举报
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//第一种

PrintWriter writer = response.getWriter();  

       Map<String, String[]> params = request.getParameterMap();  
       String queryString = "";  
       for (String key : params.keySet()) {  
           String[] values = params.get(key);  
           for (int i = 0; i < values.length; i++) {  
               String value = values[i];  
               queryString += key + "=" + value + "&";  
           }  

       }  

       // 去掉最后一个空格  
      // queryString = queryString.substring(0, queryString.length() - 1);  
       writer.println("POST " + request.getRequestURL() + " " + queryString); 

       System.out.println(new Date()+"\n"+queryString);

//第二中 JSON字符串

System.out.println(new Date()+"\n");
InputStream sin = new BufferedInputStream(request.getInputStream());
ByteArrayOutputStream sout = new ByteArrayOutputStream();
int b=0;
while((b=sin.read())!=-1)
{
sout.write(b);
}
byte[] temp = sout.toByteArray();
String s_ok = new String(temp,"UTF-8");

System.out.println(s_ok);


/*Map<String, String[]> map = request.getParameterMap();
JSONObject jsob=new JSONObject();
Iterator<String> keyit = map.keySet().iterator();
while (keyit.hasNext()) {
   String key = keyit.next();
   String value = "[" + map.get(key)[0] + "]";
  System.out.println(value);
 
}*/

}


http://blog.csdn.net/moneyshi/article/details/24986125

http://blog.csdn.net/zhejingyuan/article/details/9180885

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3398次
    • 积分:71
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档