Netty:Http多部分内容的头详细信息与文件内容混合在一起
在从FullHttpRequest检索内容时,我仍然可以在内容中看到http头数据-Netty:Http多部分内容的头详细信息与文件内容混合在一起,netty,multipart,Netty,Multipart,在从FullHttpRequest检索内容时,我仍然可以在内容中看到http头数据- $> cat /tmp/ABC ------------------------------c9f68c9ab255 Content-Disposition: form-data; name="file"; filename="file_to_upload" Content-Type: application/octet-stream abcd ---------------------------
$> cat /tmp/ABC
------------------------------c9f68c9ab255
Content-Disposition: form-data; name="file"; filename="file_to_upload"
Content-Type: application/octet-stream
abcd
------------------------------c9f68c9ab255--
我正在使用curl提交上传-
$> cat /tmp/file_to_upload
abcd
$> curl -F "file=@/tmp/file_to_upload" -X POST http://<host>:<port>/upload
在MyFullHttpHandler()中,我像这样保存上传的文件-
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;
FileChannel fileChannel = new FileOutputStream("/tmp/ABC").getChannel();
ByteBuffer buffer = request.content().nioBuffer();
try {
while (buffer.hasRemaining()) {
fileChannel.write(buffer);
}
} catch (Exception e) {
System.out.println(e);
} finally {
fileChannel.close();
}
}
}
我在没有HttpObjectAggregator的情况下进行了尝试,并使用了类似的技术,内容似乎是正确的
我是否错误地使用了HttpObjectAggregator?这是一个已知的问题吗?谢谢你的帮助
==更新===
我没有在内容上使用解码器。下面几行代码修复了我遇到的问题-
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(true), req);
InterfaceHttpData data = decoder.next();
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(true), req);
InterfaceHttpData data = decoder.next();
您可以使用DiskFileUpload将上传的数据保存到文件中。我没有在内容上使用解码器。下面几行代码修复了我遇到的问题-
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(true), req);
InterfaceHttpData data = decoder.next();
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(true), req);
InterfaceHttpData data = decoder.next();
查看“===更新===”后的答案您应该将其添加为答案,而不是将其附加到问题中。