我对netty很陌生,我遵循这个示例使用netty编写了一个静态文件服务器。但是每当服务器提供一个大的js文件时。它会运行到ClosedChannelException。
下面是我的代码,我将chunkedFile写成http响应。当一个大的js文件被提供时,我会得到closedChannelException,并且raf文件也会关闭。
你能帮我找出我在这里做错了什么吗?此外,是否有一个简单的教程,让我了解网络控制的基本流程?
// 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);
}
}<
发布于 2013-08-04 18:18:24
在finally块中调用raf.close()是错误的,因为它可能还没有编写它。实际上,netty会注意在写完之后关闭它。
https://stackoverflow.com/questions/18042923
复制相似问题