我是 Netty 新手,我遵循了这个 example使用netty编写静态文件服务器。但是每当服务器提供一个大的js文件时。它遇到 ClosedChannelException。
以下是我的代码,我将 chunkedFile 写入作为 http 响应。 当提供大型 js 文件时,我收到 closeChannelException 并且 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);
}
}
<
最佳答案
在finally block 中调用raf.close()是错误的,因为它可能还没有写入。事实上,netty 会在写入完成后关闭它。
关于java - Netty ClosedChannelException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18042923/