0

我正在构建一个客户端/服务器上传应用程序。客户端将向服务器发送多个大文件。

我已经找到了 ChunkedInput 类,例如 ChunkedFile,它只将文件的大块发送到服务器。客户端只将 ChunkedWriteHandler 添加到管道中,然后逐个发送文件(这是正确的吗?)

//Client snippets
public void initChannel(SocketChannel ch) throws Exception {
  ch.pipeline().addLast(new ChunkedWriteHandler());
}
public void send(Channel channel, List<File> files){
    for (Iterator<File> iterator = files.iterator(); iterator.hasNext();) {
        File file = iterator.next();
        channel.write(file.length());
        ChannelFuture cf = channel.writeAndFlush(new ChunkedFile(file));
        cf.await();
    }

但我到底在服务器端做什么?我怎样才能实现第一个文件的结尾?ChunkedFile 没有发送带有其块的文件大小,对吗?也许有人可以为服务器端发布代码片段?提前致谢

4

1 回答 1

0

Well i found the solution by myself.

The problem seems to be the

channel.write(file.length());

I have to write a ChunkedInput to the channel.

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(file.length());
dos.flush();
channel.write(new ChunkedStream(new ByteArrayInputStream(baos.toByteArray())));
于 2013-09-18T07:12:15.433 回答