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

    }

  • 相关阅读:
    Atitit.异步编程技术原理与实践attilax总结
    AjaxToolKit之Rating控件的使用(http://www.soaspx.com/dotnet/ajax/ajaxtech/ajaxtech_20091021_1219.html)
    JavaScript初学指南
    javascript泛型集合类(转)
    HTTP 错误 404.2 Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面
    IIS连接oralce数据库时 提示“System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本”
    配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler” 解决办法
    self.location.href的具体用法(转)
    CSS Overflow属性详解(转)
    .net中使用showModalDialog打开模式窗口,在后台代码中使用Response.Write()会弹出新页面
  • 原文地址:https://www.cnblogs.com/yy00/p/9000956.html
Copyright © 2011-2022 走看看