如何在处理程序中正确读取POST请求正文?
我现在使用的代码:
Pooled<ByteBuffer> pooledByteBuffer = exchange.getConnection().getBufferPool().allocate();
ByteBuffer byteBuffer = pooledByteBuffer.getResource();
int limit = byteBuffer.limit();
byteBuffer.clear();
exchange.getRequestChannel().read(byteBuffer);
int pos = byteBuffer.position();
byteBuffer.rewind();
byte[] bytes = new byte[pos];
byteBuffer.get(bytes);
String requestBody = new String(bytes, Charset.forName("UTF-8") );
byteBuffer.clear();
pooledByteBuffer.free();
它似乎工作正常,但我不确定在将其返回到池之前是否需要ByteBuffer。我甚至不确定是否使用.文档中没有太多关于它的内容。clear()
exchange.getConnection().getBufferPool().allocate();