【问题标题】:Netty: Http multipart content has header details mixed with file contentNetty:Http 多部分内容具有与文件内容混合的标题详细信息
【发布时间】:2017-08-15 01:00:47
【问题描述】:

在从 FullHttpRequest 检索内容时,我仍然在内容中看到 http 标头数据 -

$> 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 将上传的数据保存到文件中。

【问题讨论】:

  • 在'=== UPDATE ==='之后查看答案
  • 您应该将其添加为答案,而不是将其附加到问题中。

标签: netty multipart


【解决方案1】:

我没有对内容使用解码器。以下几行解决了我遇到的问题 -

HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(true), req);
InterfaceHttpData data = decoder.next();

【讨论】:

    猜你喜欢
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 2016-07-18
    • 2014-09-07
    • 1970-01-01
    • 2012-07-25
    • 2016-06-22
    相关资源
    最近更新 更多