Netty:Http multipart内容包含与文件内容混合的标题详细信息

时间:2017-03-22 18:16:11

标签: netty multipart

在从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?这是一个已知的问题?感谢任何帮助。

=== UPDATE ===

我没有在内容上使用解码器。以下几行修复了我遇到的问题 -

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

您可以使用DiskFileUpload将上传的数据保存到文件中。

1 个答案:

答案 0 :(得分:0)

我没有在内容上使用解码器。以下几行修复了我遇到的问题 -

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body>
    <label>Search: <input type="text" ng-model="searchKeyword"></label>
    <div ng-app="myApp" ng-controller="customersCtrl"> 
        <table>
          <tr ng-repeat="x in names | filter:searchKeyword">
            <td>{{ x.Name }}</td>
            <td>{{ x.Country }}</td>
            <td>{{ x.City }} </td>
          </tr>
        </table>
    </div>

    <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope) {
        $scope.names = [
        {"Name":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"}, 
        {"Name":"Ana Trujillo Emparedados y helados","City":"México D.F.","Country":"Mexico"}, 
        {"Name":"Antonio Moreno Taquería","City":"México D.F.","Country":"Mexico"}, 
        {"Name":"Around the Horn","City":"London","Country":"UK"}, 
        {"Name":"B's Beverages","City":"London","Country":"UK"}, 
        {"Name":"Berglunds snabbköp","City":"Luleå","Country":"Sweden"}, 
        {"Name":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"}, 
        {"Name":"Blondel père et fils","City":"Strasbourg","Country":"France"}, 
        {"Name":"Bólido Comidas preparadas","City":"Madrid","Country":"Spain"}, 
        {"Name":"Bon app'","City":"Marseille","Country":"France"}, 
        {"Name":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"}, 
        {"Name":"Cactus Comidas para llevar","City":"Buenos Aires","Country":"Argentina"}, 
        {"Name":"Centro comercial Moctezuma","City":"México D.F.","Country":"Mexico"}, 
        {"Name":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"}, 
        {"Name":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"}
        ];
    });
    </script>
</body>
</html>