原文地址

http://my.oschina.net/momohuang/blog/114552

先说一下,本来是想自己写socket ,启动一个简单点的web服务用于接收数据的。写完之后,发现会有各种情况没有考虑到的,很有可能出现问题,而且,太折腾了。于是,就用了netty去其web服务,另外,我也觉得netty基本上是最简单的web服务了吧,如果童鞋们有其他推荐的话,就留个言呗。

1、server

public class AdminServer {
protected static final Log log = LogFactory.getLog(AdminServer.class);
public static void main(String[] args) {
log.info("start app");
start(8088);
// System.out.println("admin start on "+1);
} public static void start(int port) {
// 配置服务器-使用java线程池作为解释线程
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
// 设置 pipeline factory.
bootstrap.setPipelineFactory(new ServerPipelineFactory());
// 绑定端口
bootstrap.bind(new InetSocketAddress(port));
System.out.println("admin start on "+port);
ServiceLocator.initServiceLocator();
} private static class ServerPipelineFactory implements
ChannelPipelineFactory {
public ChannelPipeline getPipeline() throws Exception {
// Create a default pipeline implementation.
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("encoder", new HttpResponseEncoder()); //http处理handler
pipeline.addLast("handler", new AdminServerHandler());
return pipeline;
}
}
}
 
 

启动了服务,绑定了8088端口。

2、当客户端给服务端post数据的时候,如果数据超过50K,这个时候服务端接受到的post内容是空的了。这是因为超过了 服务端默认的post的长度的最大值。

http协议里边,本来是没有对post的长度进行限制,但是,无论是系统层面或者是服务端层面的,都会对post的长度进行限制,这个也有利于网络安全。

3、在netty中的解决方法

private static class ServerPipelineFactory implements
ChannelPipelineFactory {
public ChannelPipeline getPipeline() throws Exception {
// Create a default pipeline implementation.
ChannelPipeline pipeline = Channels.pipeline();
// pipeline.addFirst("frameDecoder", new LengthFieldBasedFrameDecoder(100000000,0,4,0,4));
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("encoder", new HttpResponseEncoder());
// pipeline.addLast("streamer", new ChunkedWriteHandler());
pipeline.addLast("aggregator", new HttpChunkAggregator(65536));//设置块的最大字节数
//http处理handler
pipeline.addLast("handler", new AdminServerHandler());
return pipeline;
}
}
 
 

加上

pipeline.addLast("aggregator", new HttpChunkAggregator(65536))
 
 

之后,设置默认的chunk最大为 65536,这样,就可以接受最大post的内容大小为 65536。

这样有一个不好的地方,就是这个大小不好控制,开大了,会浪费空间。并且在接受到的字符串的最后,会出现空白的字符串,这是由于post的内容长度小于chunk里边的ChannelBuffer的数组的大小,程序给予补全。

4、自己设置,自己读取chunk

加上

pipeline.addLast("streamer", new ChunkedWriteHandler());
 
 

设置为给位 分开一个个chunk去接受信息。

public boolean excuteChunk(ChannelHandlerContext ctx, MessageEvent e)
throws TooLongFrameException {
// HttpMessage currentMessage = e.getMessage(); if (e.getMessage() instanceof HttpMessage) {
HttpMessage m = (HttpMessage) e.getMessage();
if (m.isChunked()) {
// A chunked message - remove 'Transfer-Encoding' header,
// initialize the cumulative buffer, and wait for incoming
// chunks.
List<String> encodings = m
.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
encodings.remove(HttpHeaders.Values.CHUNKED);
if (encodings.isEmpty()) {
m.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);
}
m.setContent(ChannelBuffers.dynamicBuffer(e.getChannel()
.getConfig().getBufferFactory()));
this.currentMessage = m;
} else {
// Not a chunked message - pass through.
this.currentMessage = null;
}
return false;
} else if (e.getMessage() instanceof HttpChunk) {
// Sanity check
if (currentMessage == null) {
throw new IllegalStateException("received "
+ HttpChunk.class.getSimpleName() + " without "
+ HttpMessage.class.getSimpleName());
} // Merge the received chunk into the content of the current message.
HttpChunk chunk = (HttpChunk) e.getMessage();
ChannelBuffer content = currentMessage.getContent(); if (content.readableBytes() > maxContentLength
- chunk.getContent().readableBytes()) {
throw new TooLongFrameException("HTTP content length exceeded "
+ maxContentLength + " bytes.");
} content.writeBytes(chunk.getContent());
if (chunk.isLast()) {
this.currentMessage = null;
currentMessage.setHeader(HttpHeaders.Names.CONTENT_LENGTH,
String.valueOf(content.readableBytes()));
return true;
// Channels.fireMessageReceived(ctx, currentMessage,
// e.getRemoteAddress());
}
}
return true;
}
 
 

在handle中,自己做处理,接受 整个post过来的数据,然后在整合起来,即可

解决用netty去做web服务时,post长度过大的问题的更多相关文章

  1. idea 为模块添加Tomcat依赖 解决: Intelij IDEA 创建WEB项目时没有Servlet的jar包

    解决: Intelij IDEA 创建WEB项目时没有Servlet的jar包 今天创建SpringMVC项目时 用到HttpServletRequest时, 发现项目中根本没有Servlet这个包, ...

  2. 解决: Intelij IDEA 创建WEB项目时没有Servlet的jar包

    今天创建SpringMVC项目时 用到HttpServletRequest时, 发现项目中根本没有Servlet这个包, 在网上搜了一下,这个问题是因为web项目没有添加服务器导致的. 配置tomec ...

  3. 解决eclipse中运行web项目时弹出的"Port 8080 required by Tomcat 9.0 Server at localhost is already in use...

    1.tomcat默认端口是8080,可以修改通过tomcat的端口 修改tomcat\conf\server.xml     结果运行程序,还是报"Port 8080 required by ...

  4. ASP.NET Web服务调用发生错误,错误代码404

    现象: iOS端使用ASIHTTP连接Web服务时,得到的数据是一个错误代码为404的页面,错误信息(web.config添加<customErrors mode="Off" ...

  5. 服务设计模式一:Web服务概述

    目录 1. Web服务是什么 2.为什么要使用Web服务 3.Web服务考虑的因素和替代方案 4.SOA是什么 Web服务是什么? 所谓服务,通俗的理解就是别人帮你做一些事情,比如说,腰酸背痛了,找个 ...

  6. Web服务端性能提升实践

    随着互联网的不断发展,日常生活中越来越多的需求通过网络来实现,从衣食住行到金融教育,从口袋到身份,人们无时无刻不依赖着网络,而且越来越多的人通过网络来完成自己的需求. 作为直接面对来自客户请求的Web ...

  7. Linux实战教学笔记37:企业级Nginx Web服务优化实战(上)

    一,Nginx基本安全优化 1.1 调整参数隐藏Nginx软件版本号信息 一般来说,软件的漏洞都和版本有关,这个很像汽车的缺陷,同一批次的要有问题就都有问题,别的批次可能就都是好的.因此,我们应尽量隐 ...

  8. 踩坑踩坑之Flask+ uWSGI + Tensorflow的Web服务部署

    一.简介 作为算法开发人员,在算法模块完成后,拟部署Web服务以对外提供服务,从而将算法模型落地应用.本文针对首次基于Flask + uWSGI + Tensorflow + Nginx部署Web服务 ...

  9. Python Web 服务开发者: 第 1 部分

    Python Web 服务开发者: 第 1 部分 Python Web 服务世界 Python 的座右铭一向是“装备齐全”,这是指在安装该语言时会附带一大套标准库和功能程序.本文概述了在 Python ...

  10. 利用OpenShift托管Node.js Web服务进行微信公众号开发

    最近写了一个微信的翻译机器人.用户只要关注该公众号,发送英文的消息,就能收到中文翻译的回复.有兴趣的读者可以扫描下面的二维码关注该公众号,尝试发送英文单词试试看.(有时候第一次发送单词会收到“该公众号 ...

随机推荐

  1. 系统 内核启动期间使用ftrace

    启动阶段使能event trace 同上,配置commandline: trace_event=sched:*,timer:*,irq:* trace_buf_size=40M 有上面的实例可以知道, ...

  2. 4.3.2 等比数列的前n项和公式

    \({\color{Red}{欢迎到学科网下载资料学习 }}\) [ [基础过关系列]高二数学同步精品讲义与分层练习(人教A版2019)] ( https://www.zxxk.com/docpack ...

  3. docker部署Prometheus与Grafana

    prometheus部署 建立文件 mkdir -p /ops/prometheus-data && cd /ops/prometheus-data vi /ops/prometheu ...

  4. 阿里云服务器安装mysql镜像

    新创建的服务器首先需要创建安全组,开放端口然后重启服务器 登陆远程服务器,具体操作步骤如下 #拉取镜像 docker pull mysql:5.7 #查看镜像是否拉取到 docker images # ...

  5. 还在使用昂贵的虚拟机?来试试 Devbox,便宜 6 倍!

    这篇小短文来介绍一下用虚拟机的场景是怎么被 Devbox 全方位碾压的. Devbox 唯一弱点是公网出口的地方不分配独立的 IP 地址,但是这对我们绝大多数场景是没有影响的,通过域名和端口访问我们的 ...

  6. Kubernetes 跨 StorageClass 迁移 Persistent Volumes 完全指南

    大家好,我是米开朗基杨. KubeSphere 3.3.0 (不出意外的话~)本周就要 GA 了,作为一名 KubeSphere 脑残粉,我迫不及待地先安装 RC 版尝尝鲜,一顿操作猛如虎开启所有组件 ...

  7. 马斯克对于CEO职能,发挥人才天赋,激励人才的想法

    Time Interview with Elon Musk, 29 September 2011. Content 1 Have people do be focused on doing usefu ...

  8. P9119 [春季测试 2023] 圣诞树

    参考博客: 春季测试 2023] 圣诞树 题解 - 洛谷专栏 (luogu.com.cn) 题意:给定二维平面上一个凸多边形的 \(n\) 个顶点, 求一种方案,使得从最高点开始,不重复地经过所有点后 ...

  9. 反思---树上LIS

    反思---树上LIS 题目描述 给你一棵 n个节点的树,树的每个节点上都有一个值 a[i] . 现在要您求出从 1 号点到 i 号点的最短路径上最长上升子序列的长度. 就是单调栈优化+dfs回溯 对比 ...

  10. 4G模组软件指南 | json数据处理深度学习篇

    针对4G模组软件的json数据处理,我已做出如下示例分享给大家,以4G模组Air780E为例: 1.JSON介绍 JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式 ...