Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Netty:如何避免在一个BiteBuf中接收文件块和其他消息_Java_Netty_File Transfer - Fatal编程技术网

Java Netty:如何避免在一个BiteBuf中接收文件块和其他消息

Java Netty:如何避免在一个BiteBuf中接收文件块和其他消息,java,netty,file-transfer,Java,Netty,File Transfer,我正试图通过Netty发送文件。发送文件,最后发送“结束文件传输”消息,以通知客户端文件已完全发送。代码如下: File file = new File( "/file/to/pdf/test.pdf" ); RandomAccessFile raf = new RandomAccessFile(file, "r"); ChannelFuture channelFuture = ctx.write(new DefaultFileRegion(raf.getChannel(), 0, raf.le

我正试图通过Netty发送文件。发送文件,最后发送“结束文件传输”消息,以通知客户端文件已完全发送。代码如下:

File file = new File( "/file/to/pdf/test.pdf" );
RandomAccessFile raf = new RandomAccessFile(file, "r");
ChannelFuture channelFuture = ctx.write(new DefaultFileRegion(raf.getChannel(), 0, raf.length())

channelFuture.addListener( new ChannelFutureListener(){
    @Override public void operationComplete(ChannelFuture future) throws Exception{
        ctx.writeAndFlush( CommonClientDefines.END_FILE_TRANSFER.getBytes() );
    }
}
不幸的是,客户端在一个
ByteBuf
中接收到文件内容的最后一块和“结束文件传输”消息

public class FileChunkHandler extends SimpleChannelInboundHandler<ByteBuf>{
    @Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception{
    }
}
公共类FileChunkHandler扩展了SimpleChannelInboundHandler{
@重写受保护的无效channelRead0(ChannelHandlerContext ctx,ByteBuf msg)引发异常{
}
}
我猜这是因为传入的消息在转发到管道中的通道之前也在客户端上进行缓冲。
有什么办法解决这个问题吗?当然,我可以解析结束消息的消息,但是如果有其他解决方案,我会感兴趣的

您有一个定义不好的协议。当使用TCP时,您总是会得到一堆字节。如果您想要数据包和其他数据包,您需要自己解析字节。您有一个定义不好的协议。当使用TCP时,您总是会得到一堆字节。如果您想拥有数据包和其他数据包,您需要自己解析字节。