Netty:HTTP内容长度超过64K时出错

时间:2012-08-31 07:51:23

标签: netty

我从客户端应用程序接收JSON数据,但偶尔HTTP内容长度超过64K,我收到以下错误:

org.jboss.netty.handler.codec.frame.TooLongFrameException: HTTP content length exceeded 65536 bytes.

我目前有以下相当天真的实现来读取HTTP内容:

String requestContent = null;
HttpRequest request = (HttpRequest) e.getMessage();
ChannelBuffer content = request.getContent();
if (content.readable()) {
    requestContent = content.toString(CharsetUtil.UTF_8);
}

有没有办法启用接收64K以上的数据?

编辑:堆栈跟踪:

Aug 31, 2012 2:35:20 PM org.jboss.netty.channel.SimpleChannelUpstreamHandler
WARNING: EXCEPTION, please implement org.eurekaj.manager.server.router.RouterHandler.exceptionCaught() for proper handling.
org.jboss.netty.handler.codec.frame.TooLongFrameException: HTTP content length exceeded 65536 bytes.
at org.jboss.netty.handler.codec.http.HttpChunkAggregator.messageReceived(HttpChunkAggregator.java:130)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.unfoldAndFireMessageReceived(ReplayingDecoder.java:593)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:584)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:509)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:94)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processSelectedKeys(AbstractNioWorker.java:372)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:246)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:38)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

这再次导致我的应用程序出现以下错误,表明只从HTTP请求中读取了第一个64K数据:

Caused by: org.json.JSONException: Unterminated string at character 65537
at org.json.JSONTokener.syntaxError(JSONTokener.java:410)
at org.json.JSONTokener.nextString(JSONTokener.java:244)

1 个答案:

答案 0 :(得分:6)

好的,然后在HttpChunkAggregator的构造函数中指定不同的最大内容长度。这应该可以解决问题..