1
我目前正在做一个学习netty的项目,我正在尝试使用ChunkedFileHandler将文件从服务器发送到客户端。因为它是在控制台中记录 服务器端代码中的文件被发送阅读Chunked文件Netty Java
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("In channel and the context is " + ctx);
ctx.writeAndFlush(new ChunkedFile(new File("Test//ige.dat")));
}
该文件被发送。 但现在回到客户端,我的记录器说我正在接收1024字节的文件,这是绝对好的。
public class ChunkedFileClientHandler extends SimpleChannelInboundHandler<ChunkedFile> {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
System.out.println("Read a chunk of file");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("Chunk read completed!");
}
该文件服务器发送的是239 mb的。每次接收到一个数据块时,首先启动channelRead并触发channelReadComplete。
现在我的问题是我将如何组装这些块,并再次形成文件的名称和其他所有信息完好无损。我查看了netty网站,但他们没有读取分块文件的客户端代码。
谢谢
好吧,我明白了,但是您对如何将文件块组装成可读文件有任何想法吗? – Sneh 2015-04-04 18:05:14
查看更新的答案。 – yole 2015-04-04 18:25:53
感谢您的回答。我现在可以在客户端重新创建文件。只有最后一件我想知道的是,有没有关于如何创建自定义协议的任何教程? Netty教程并不完全清楚,我不知道如何使用协议传输文件名。 – Sneh 2015-04-04 18:52:34