我可以看到两个部分:
1)您不应在自己的处理程序中创建新的处理程序,而应直接创建ChunkedFile并编写它:
public class FileRequestServerHandler extends SimpleChannelInboundHandler<FileRequestProtocol> { private File f; private Logger logger = Logger.getLogger(this.getClass()); @Override public void channelRead0(ChannelHandlerContext ctx, FileRequestProtocol fileRequest) { logger.info("Server new FileRequest " + fileRequest); f = new File(fileRequest.getFilePath()); fileRequest.setFileSize(f.length()); ctx.writeAndFlush(fileRequest); // directly make your chunkedFile there instead of creating a sub handler chunkedFile = new ChunkedFile(this.file); ctx.writeAndFlush(chunkedFile);// need a specific handler // Don't create such an handler: new ChunkedFileServerHandler(ctx,f);}
2)由于使用ChunkedInput(此处为ChunkedFile)编写,因此在处理程序之前必须在管道中具有ChunkedWriteHandler,因此Initializer看起来像:
public class ServerInitializer extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("enprer", new ObjectEnprer()); p.addLast("deprer", new ObjectDeprer(ClassResolvers.cacheDisabled(null))); p.addLast("chunkedWriteHandler", new ChunkedWriteHandler());// added p.addLast("protocolhead", new ProtocolHeadServerHandler()); p.addLast("filerequestserverhandler", new FileRequestServerHandler()); // removed: p.addLast("chunkedfileserver", new ChunkedFileServerHandler()); }}
的位置
ChunkedWriteHandler可以更改,但始终在编写的自己的处理程序之前
ChunkedFile。
3)最后的注意事项:看一下并照顾好您的编码器/解码器(ObjectEnprer /
ObjectDeprer),因为我不确定100%是否可以与这样的从文件到文件的ByteBuf读/写协作。它可能起作用,或者不起作用…
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)