Http 如何使用基于netty 4.0的客户端进行PUT?

Http 如何使用基于netty 4.0的客户端进行PUT?,http,netty,Http,Netty,我想使用netty发送一个PUT请求,但从服务器获得一个超时。似乎尸体没有被送到。这是我的代码: // open file to PUT RandomAccessFile file = new RandomAccessFile("myFile", "r"); long fileLength = file.length(); // create request (without content, i.e. no DefaultFullHttpRequest) HttpRequest reques

我想使用netty发送一个PUT请求,但从服务器获得一个超时。似乎尸体没有被送到。这是我的代码:

// open file to PUT
RandomAccessFile file = new RandomAccessFile("myFile", "r");
long fileLength = file.length();

// create request (without content, i.e. no DefaultFullHttpRequest)
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, path);
HttpHeaders headers = request.headers();
headers.set(HttpHeaders.Names.HOST, host);
headers.set(HttpHeaders.Names.CONTENT_LENGTH, fileLength);

// send request
ch.write(request);

// send body - is this correct?
ch.write(new DefaultFileRegion(file.getChannel(), 0, fileLength), ch.newProgressivePromise());
ch.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

// wait
ch.closeFuture().sync();
如果这是可行的,当然应该使用100 continue。
谢谢您的提示。

您应该使用
DefaultFullHttpRequest
而不是
DefaultHttpRequest


这样,您可以使用方法
DefaultFullHttpRequest.content()
获取表示请求内容的
ByteBuf
。只需在发送请求之前通过调用
ch.writeAndFlush(request)

将数据写入其中,就好像它可以与ch.writeAndFlush(new-httpchunkedput(new-ChunkedFile(file,0,fileLength,8192)),ch.newProgressivePromise()一起工作一样;即使我的请求没有被分块。ChunkedWriteHandler必须位于HttpClientCodec之后的管道中。ChunkedFile与RandomAccessFile配合使用,但我更喜欢FileChannel,因此我必须实现ChunkedFileChannel,因为我不知道如何使用DefaultFileRegion。感谢您的回复,但我正在寻找一种使用文件引用而不是内存缓冲区的解决方案,以便上传非常大的文件