关闭

java 获取 post参数

标签: java获取post请求参数
232人阅读 评论(0) 收藏 举报
分类:
/**  
 * @Title: GgCzyTBServlet.java
 * @Package com.zzxy.common.gg_jgtb.servlet
 * @Description: TODO
 * @author 屈卞忠
 * @date 2016-8-22 下午05:02:03
 */
package com.zzxy.common.gg_czytb.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zzxy.common.gg_czytb.iface.CzytbFacade;
import com.zzxy.common.gg_jgtb.iface.JgtbFacade;
import com.zzxy.common.util.SpringBeanUtil;

/**
 * ClassName: GgCzyTBServlet 
 * @Description: TODO
 * @author 屈卞忠
 * @date 2016-8-22 下午05:02:03
 */
@SuppressWarnings("serial")
public class GgCzyTBServlet extends HttpServlet {
	CzytbFacade czytbFacade = (CzytbFacade) SpringBeanUtil.getBean("czy.CzytbFacade");

	/**
	 * 新增用户数据
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String str = this.getData(request);
		HashMap<String, String> map = this.jsonForMap(str);
		
		czytbFacade.addOrUpdateCzy(map.get("userId").toString(),map.get("username").toString(),map.get("orgId").toString()
				,map.get("phone").toString(),map.get("workTel").toString());
	}

	/**
	 * 更新用户数据
	 */
	public void doPut(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	/**
	 * 
	 * @Description: TODO 获取请求传输过来的数据
	 * @param @param req
	 * @param @return
	 * @param @throws IOException   
	 * @return String  
	 * @throws
	 * @author 屈卞忠
	 * @date 2016-8-25 上午09:36:14
	 */
	private String getData(HttpServletRequest req) throws IOException {
		// 获取post参数
		StringBuffer sb = new StringBuffer();
		InputStream is = req.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		String s = "";
		while ((s = br.readLine()) != null) {
			sb.append(s);
		}
		return  sb.toString();
	}
	/**
	 * 
	 * @Description: TODO 将json字符串转换为hashmap
	 * @param @param str
	 * @param @return   
	 * @return HashMap<String,String>  
	 * @throws
	 * @author 屈卞忠
	 * @date 2016-8-26 下午02:38:02
	 */
	private HashMap<String, String> jsonForMap(String str) {
		/*
		{"data":{"userId":"0002","username":"朱玉国","orgId":"100","orgName":"长沙3银行总行","deptId":"","position":"董事长","phone":"18807312207","workTel":"54221","shortTel":"101","displayOrder":1,"enable":true},"secret":"7cb32c5c98344646b6a9c9ac7caf5245"}
		*/
		HashMap<String, String> map = new HashMap<String, String>();
		String data_ = str.substring(1, str.length()-1);
		System.out.println(data_);
		String data1_ = data_.split("}")[0];
		String data2_ = data1_.substring(8);
		String [] data = data2_.split(",");
		for(int i=0 ;i<data.length;i++){
			String[] s_ = data[i].split(":");
			String key=s_[0].substring(1, s_[0].length()-1);
			String value = "";
			if(s_[1].contains("\"")){
				value = s_[1].substring(1, s_[1].length()-1);
			}else{
				value = s_[1];
			}
			map.put(key, value);
		}
		return map;
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:74260次
    • 积分:2124
    • 等级:
    • 排名:第18587名
    • 原创:124篇
    • 转载:59篇
    • 译文:1篇
    • 评论:5条