Netty:Http 多部分内容有 header 细节与文件内容混合
Netty: Http multipart content has header details mixed with file content
从 FullHttpRequest 检索内容时,我仍然在内容中看到 http header 数据 -
$> 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
我正在使用 Netty-4.1。9.Final。我为我的 NettyServer 设置了这些处理程序 -
channel.pipeline().addLast(new HttpServerCodec());
channel.pipeline().addLast(new HttpObjectAggregator(1048576));
channel.pipeline().addLast(new MyFullHttpHandler());
在 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 的情况下进行了尝试,并使用了类似于 here 的技术,内容似乎是正确的。
我是否错误地使用了 HttpObjectAggregator?这是一个已知的问题?感谢任何帮助。
===更新===
我没有对内容使用解码器。以下几行解决了我遇到的问题 -
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();
从 FullHttpRequest 检索内容时,我仍然在内容中看到 http header 数据 -
$> 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
我正在使用 Netty-4.1。9.Final。我为我的 NettyServer 设置了这些处理程序 -
channel.pipeline().addLast(new HttpServerCodec());
channel.pipeline().addLast(new HttpObjectAggregator(1048576));
channel.pipeline().addLast(new MyFullHttpHandler());
在 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 的情况下进行了尝试,并使用了类似于 here 的技术,内容似乎是正确的。
我是否错误地使用了 HttpObjectAggregator?这是一个已知的问题?感谢任何帮助。
===更新===
我没有对内容使用解码器。以下几行解决了我遇到的问题 -
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();