【发布时间】:2013-08-05 02:18:24
【问题描述】:
我是netty的新手,我按照example使用netty编写了一个静态文件服务器。但是每当服务器提供大型 js 文件时。它遇到了 ClosedChannelException。
以下是我编写 chunkedFile 作为 http 响应的代码。 当提供一个大的 js 文件时,我得到了 closedChannelException 并且 raf 文件也被关闭了。
你能帮我弄清楚我在这里做错了什么吗?另外,有没有简单的教程让我了解netty中的基本控制流程?
// Write the content. ChannelFuture writeFuture = null; try { long fileLength = raf.length(); HttpResponse response = new DefaultHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, fileLength); Channel c = ctx.getChannel();
// Write the initial line and the header.
c.write(response);
writeFuture = c.write(new ChunkedFile(raf, 0, fileLength, 8192));
}
finally
{
raf.close();
if (writeFuture != null)
writeFuture.addListener(ChannelFutureListener.CLOSE);
}
}
【问题讨论】: