当前位置 : 主页 > 编程语言 > c++ >

对post请求参数的解析

来源:互联网 收集:自由互联 发布时间:2021-07-03
gistfile1.txt package com.yxcps.logs.logger;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.net.URLDecoder;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.Str
gistfile1.txt
package com.yxcps.logs.logger;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

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

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import com.google.gson.Gson;
import com.yxcps.logs.LogsFilter;
import com.yxcps.logs.diagnostics.logging.Logger;
import com.yxcps.logs.diagnostics.logging.Loggers;
import com.yxcps.logs.utils.RequestHelper;

/**
 * 
 * POST
 * 
 * http://log.yxcps.cn/__log.action?_=09090909090
 * 
 */
public class AjaxLogger {

	private static final Logger loggerPerHour = Loggers.getLogger("per.hour");

	private static final Logger loggerDailyLog = Loggers.getLogger("daily.log");

	private static final Logger debugLog = Loggers.getLogger(LogsFilter.class.getName());

	private Gson gson = new Gson();

	public void recordAjaxLog(HttpServletRequest request, HttpServletResponse response) {
		recordAjaxLog(request);
		outHtml(response);
	}

	private void recordAjaxLog(HttpServletRequest request) {
		if (request.getMethod().toUpperCase().equals("POST")) {
			try {
				Map
 
   document = new HashMap
  
   (); InputStream input = request.getInputStream(); String str = IOUtils.toString(input); StringTokenizer st = new StringTokenizer(str, "&"); while (st.hasMoreElements()) { String[] s = st.nextToken().split("="); if (s.length == 1) { document.put(s[0], ""); } else if (s.length == 2) { String name = s[0]; String value = s[1]; value = URLDecoder.decode(value, "UTF-8"); document.put(name, value); } else { // System.out.println(Arrays.asList(s)); } } document.put("ip", RequestHelper.getIpAddr(request)); String write_time = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"); document.put("m1", "" + Long.parseLong(write_time)); String json = gson.toJson(document); loggerPerHour.info(json); document.put("M17", ""); document.put("M4", ""); json = gson.toJson(document); loggerDailyLog.info(json); } catch (Exception e) { debugLog.info(e.getMessage(), e); } } } private void outHtml(HttpServletResponse response){ try { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0L); PrintWriter out = response.getWriter(); out.write("{}"); out.flush(); out.close(); } catch (IOException e) { } } }
  
 
网友评论