zoukankan      html  css  js  c++  java
  • TCP 服务端接收数据解析工具类

    package com.ivchat.common.util;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.util.Map;

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

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;

    import org.apache.commons.httpclient.Cookie;
    import org.apache.commons.io.output.ByteArrayOutputStream;
    import org.apache.log4j.Logger;

    /**
    * @author 居里智能 2014-08-14
    * @version 1.0.0 Servlet工具类
    */

    public class ServletUtil {
    private static Logger logger = Logger.getLogger(ServletUtil.class);
    /**
    * 直接发送消息对象
    *
    * @param rsp
    * @param msgOut
    * 待发送的消息对象
    */
    public static void send(HttpServletResponse rsp, String msgOut) {
    logger.info("-----回应客户端---"+msgOut);
    //System.out.println(msgOut);
    rsp.setCharacterEncoding("UTF-8");
    rsp.setContentType("text/html;charset=UTF-8");
    OutputStreamWriter ow = null;
    ServletOutputStream servletOut = null;
    try {
    servletOut = rsp.getOutputStream();
    ow = new OutputStreamWriter(servletOut, "UTF-8");
    ow.write(msgOut);
    } catch (IOException e) {
    logger.error("向手机客户端发送信息异常:{}"+ e.toString(),e);
    } finally {
    try {
    if (ow != null) {
    ow.close();
    }
    if (servletOut != null) {
    servletOut.close();
    }

    } catch (IOException e) {
    logger.error("向手机客户端发送信息后,释放资源异常:{}"+e.toString(),e);
    }
    }
    }
    public static String getRequestContent(HttpServletRequest request,boolean statu) {
    /*return (String) request.getAttribute("param");*/
    logger.info("-----接收到客户端信息-----" + request.getRemoteHost());
    logger.info("-----接收到客户端url-----" + request.getRequestURI());

    InputStream is = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    String retString = null;
    try {
    request.setCharacterEncoding("UTF-8");
    StringBuffer sb=new StringBuffer();
    is=request.getInputStream();
    isr=new InputStreamReader(is,"UTF-8");
    br=new BufferedReader(isr);
    String s="";
    while ((s = br.readLine()) != null) {
    sb.append(s);
    }
    //System.out.println(sb.toString());
    retString = sb.toString();
    logger.info("-----接收到客户端参数-----" + retString);
    } catch (Exception e) {
    logger.error("读取手机客户端数据异常:{}"+ e.toString(),e);
    }
    finally{
    try {
    if (br!=null) {
    br.close();
    }
    if (isr!=null) {
    isr.close();
    }
    if (is!=null) {
    is.close();
    }
    } catch (Exception e2) {
    logger.error("接收手机客户端信息后,释放资源异常:{}"+ e2.toString(),e2);
    }

    }
    return retString;
    }

    public static String getRequestContent(HttpServletRequest request) {
    /*return (String) request.getAttribute("param");*/
    logger.info("-----接收到客户端信息-----" + request.getRemoteHost());
    logger.info("-----接收到客户端url-----" + request.getRequestURI());
    InputStream is = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    String retString = null;
    try {
    request.setCharacterEncoding("UTF-8");
    StringBuffer sb=new StringBuffer();
    is=request.getInputStream();
    isr=new InputStreamReader(is,"UTF-8");
    br=new BufferedReader(isr);
    String s="";
    while ((s = br.readLine()) != null) {
    sb.append(s);
    }
    //System.out.println(sb.toString());
    retString = sb.toString();
    logger.info("-----接收到客户端参数-----" + retString);
    } catch (Exception e) {
    logger.error("读取客户端数据异常:{}"+e.toString(),e);
    }
    finally{
    try {
    if (br!=null) {
    br.close();
    }
    if (isr!=null) {
    isr.close();
    }
    if (is!=null) {
    is.close();
    }
    } catch (Exception e2) {
    logger.error("接收客户端信息后,释放资源异常:{}"+ e2.toString(),e2);
    }

    }
    return retString;
    }

    public static String processInput(InputStream input){
    byte[] response = null;
    ByteArrayOutputStream byteArrayOutput = null;
    String res = null;
    try{
    if (input != null) {
    byteArrayOutput = new ByteArrayOutputStream();
    byte[] temp = new byte[1024];
    int len = 0;
    while ((len = input.read(temp)) > 0) {
    byteArrayOutput.write(temp, 0, len);
    }
    input = null;
    response = byteArrayOutput.toByteArray();
    }
    res = new String(response, "UTF-8").trim();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    if (byteArrayOutput != null) {
    byteArrayOutput.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return res;
    }

    /**
    * 发送JSON消息
    *
    * @param rsp
    * @param msgOut
    * @author 居里智能
    */
    public static String send(HttpServletResponse rsp, Map<String, Object> jsonMap) {
    JSONArray jsonArray = JSONArray.fromObject(jsonMap);
    String respString = jsonArray.toString();
    rsp.setCharacterEncoding("UTF-8");
    rsp.setContentType("text/html;charset=UTF-8");
    OutputStreamWriter ow = null;
    ServletOutputStream servletOut = null;
    try {

    servletOut = rsp.getOutputStream();
    ow = new OutputStreamWriter(servletOut, "UTF-8");

    ow.write(respString);

    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if (ow != null) {
    ow.close();
    }
    if (servletOut != null) {
    servletOut.close();
    }

    } catch (IOException e) {
    e.printStackTrace();

    }
    }
    return respString;
    }

    /**
    * @affect 在一组cookies里面,通过name取value
    * @param cookies
    * @param cookieName
    * @return String
    * @exception 无
    */
    public static String getCookieValue(Cookie[] cookies, String cookieName) {
    if (cookies == null)
    return null;
    if (cookieName == null)
    return null;
    String cookieValue = "";
    for (int i = 0; i < cookies.length; i++) {
    if (cookieName.equals(cookies[i].getName())) {
    cookieValue = cookies[i].getValue();
    }
    }
    return cookieValue;

    }
    public static String getJsonRequestContent(HttpServletRequest request) {
    logger.info("-----接收到客户端信息-----" + request.getRemoteHost());
    logger.info("-----接收到客户端url-----" + request.getRequestURI());
    InputStream is = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    String retString = null;
    try {
    request.setCharacterEncoding("UTF-8");
    StringBuffer sb=new StringBuffer();
    is=request.getInputStream();
    isr=new InputStreamReader(is,"UTF-8");
    br=new BufferedReader(isr);
    String s="";
    while ((s = br.readLine()) != null) {
    sb.append(s);
    }
    //System.out.println(sb.toString());
    request.setAttribute("param", sb.toString());
    retString = sb.toString();
    logger.info("-----接收到客户端参数-----" + retString);
    } catch (Exception e) {
    logger.error("读取手机客户端数据异常:{}"+e.toString(),e);
    }
    finally{
    try {
    if (br!=null) {
    br.close();
    }
    if (isr!=null) {
    isr.close();
    }
    if (is!=null) {
    is.close();
    }
    } catch (Exception e2) {
    logger.error("接收手机客户端信息后,释放资源异常:{}"+ e2.toString(),e2);

    }

    }
    return retString;
    }


    /**通过request获取参数转成json
    * yehz 2015-3-30
    * @param request
    * @return
    *
    */
    public static JSONObject getParamByRequest(HttpServletRequest request){
    Map parammap= request.getParameterMap();
    JSONObject js = new JSONObject();
    if (parammap!=null&&parammap.size()>0){
    for (Object key :parammap.keySet()){
    if (key instanceof String){
    js.put(key,((Object[])parammap.get(key))[0]);
    }
    }
    }
    return js;
    }

    }

  • 相关阅读:
    Working with macro signatures
    Reset and Clear Recent Items and Frequent Places in Windows 10
    git分支演示
    The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.
    Build website project by roslyn through devenv.com
    Configure environment variables for different tools in jenkins
    NUnit Console Command Line
    Code Coverage and Unit Test in SonarQube
    头脑王者 物理化学生物
    头脑王者 常识,饮食
  • 原文地址:https://www.cnblogs.com/yy00/p/9000956.html
Copyright © 2011-2022 走看看