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&¶mmap.size()>0){
for (Object key :parammap.keySet()){
if (key instanceof String){
js.put(key,((Object[])parammap.get(key))[0]);
}
}
}
return js;
}
}
TCP 服务端接收数据解析工具类的更多相关文章
- Netty服务端接收的新连接是如何绑定到worker线程池的?
更多技术分享可关注我 前言 原文:Netty服务端接收的新连接是如何绑定到worker线程池的? 前面分析Netty服务端检测新连接的过程提到了NioServerSocketChannel读完新连接后 ...
- 读取EXCEL文档解析工具类
package test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException ...
- Java常用工具类---XML工具类、数据验证工具类
package com.jarvis.base.util; import java.io.File;import java.io.FileWriter;import java.io.IOExcepti ...
- PHP 命令行参数解析工具类
<?php/** * 命令行参数解析工具类 * @author guolinchao * @email luoyecb@163.com */class CommandLine{ // store ...
- Request模块—数据解析工具
一.爬虫基本步骤 指定URL信息 发起请求 获取响应数据 对响应数据进行数据解析 持久化存储 二.数据解析 1. 正则表达式 (1) 基本语法 1. 单字符: . : 除换行以外所有字符 [] :[a ...
- Gprinter热敏打印机光栅位图点阵数据解析工具
最近参与的项目有一个需求,解析佳博热敏打印机的光栅位图点阵数据并保存为图片文件.数据是通过Bus Hound抓取的,如下图所示. 其中1b 40为初始化打印机的指令,对应的ASCII码为ESC @,1 ...
- springmvc返回json数据的工具类
在ssm框架下,MVC向前端返回数据的json工具类代码如下: public class JsonResult<T> { public static final int SUCCESS=0 ...
- 一、JDBC的概述 二、通过JDBC实现对数据的CRUD操作 三、封装JDBC访问数据的工具类 四、通过JDBC实现登陆和注册 五、防止SQL注入
一.JDBC的概述###<1>概念 JDBC:java database connection ,java数据库连接技术 是java内部提供的一套操作数据库的接口(面向接口编程),实现对数 ...
- 安卓使用Socket发送中文,C语言服务端接收乱码问题解决方式
今天用安卓通过Socket发送数据到电脑上使用C语言写的服务端,发送英文没有问题,可当把数据改变成中文时,服务端接收到的数据确是乱码. 突然想到.VS的预处理使用的是ANSI编码.而安卓网络数据都是U ...
随机推荐
- matplotlib 3D数据-【老鱼学matplotlib】
直接上代码: import numpy as np import matplotlib.pyplot as plt # 导入显示3D的库 from mpl_toolkits.mplot3d impor ...
- python实现Hbase
1. 下载thrift 作用:翻译python语言为hbase语言的工具 2. 运行时先启动hbase 再启动thrift,最后在pycharm中通过happybase包连接hbase 在hbase目 ...
- poj1106-Post Office(DP)
Description There is a straight highway with villages alongside the highway. The highway is represen ...
- Problem J. Journey with Pigs
Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...
- 基于335X平台的UBOOT中交换芯片驱动移植
基于335X平台的UBOOT中交换芯片驱动移植 一.软硬件平台资料 1.开发板:创龙AM3359核心板,网口采用RMII形式. 2.UBOOT版本:U-Boot-2016.05,采用FDT和DM. 3 ...
- BZOJ.4299.Codechef FRBSUM(主席树)
题目链接 记mx为最大的满足1~mx都能组成的数. 考虑当前能构成1~v中的所有数,再加入一个数x,若x>v+1,则mx=v,x不会产生影响:否则x<=v+1,则新的mx=x+v. 对于区 ...
- 隐藏"Input"标签默认样式
input { width: 400px; border: none; background-color: inherit; border-bottom: #fbfee9 solid 3px; fon ...
- git 强制覆盖分支
假设要用develop覆盖master分支,如下操作 git checkout master git reset --hard develop //先将本地的master分支重置成develop gi ...
- ArcGIS JavaScript开发过程中,底图产生拼接缝问题
ArcGIS JS开发过程中,地图产生了拼接缝 上图调用的是天地图,确认原地图服务是没有这种缝隙的. 其他人电脑上测试,发现没有此问题. 纠结了半天,群里问了大神,大神说是浏览器设置了缩放.... 取 ...
- Python练手例子(7)
37.对10个数进行排序. 程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换. #python 3.7 if __n ...