Netty POST表单输入类型

时间:2015-06-18 09:35:27

标签: java forms netty

我遇到一些麻烦来从netty收到的POST中获取输入的类型。我有一个类从post请求读取所有收到的属性,我想从表单中过滤出与提交类型的输入相对应的属性。也就是说,如果我有这种形式:

<form action="https://127.0.0.1:10005/firmarMultiplesDocumentos" name="" method="POST" enctype="multipart/form-data">
  File: <input type="file" name="File1" /><br>
  NHC: <input type="text" name="NHC" value="555555" /><br>
  <input type="submit" name="Send" />
</form>

我想只获取File1和NHC属性并丢弃Send属性。

如果有帮助,这是我的频道定义:

secureBossGroup = new NioEventLoopGroup();
secureWorkerGroup = new NioEventLoopGroup();
secureServerBootstrap = new ServerBootstrap();
secureServerBootstrap.group(secureBossGroup, secureWorkerGroup)
     .channel(NioServerSocketChannel.class) // (3)
     .childHandler(new ChannelInitializer<SocketChannel>() {
          @Override
          public void initChannel(SocketChannel ch) throws Exception {
             ch.pipeline().addFirst(getSSLContext().newHandler(ch.alloc()));
             ch.pipeline().addLast(new HttpServerCodec());
             ch.pipeline().addLast("aggregator", new HttpObjectAggregator(100 * 1024 * 1024));
             ch.pipeline().addLast(new CustomChannelHandler());
          }
     })
     .option(ChannelOption.SO_BACKLOG, 128)
     .childOption(ChannelOption.SO_KEEPALIVE, true);

这是我的CustomChannelHandler的一部分:

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof FullHttpRequest) {
        FullHttpRequest request = fullHttpRequest = (FullHttpRequest) msg;
        try {
            decoder = new HttpPostRequestDecoder(dataFactory, request);
            decoder.setDiscardThreshold(0);
        } catch (Exception e) {
            // Error Handler
        }
    }

    if (decoder != null) {
        if (msg instanceof HttpContent) {
            HttpContent chunk = (HttpContent) msg;

            try {
                decoder.offer(chunk);
            } catch (ErrorDataDecoderException e) {
                //Error handler
            }

            // Read data as it becomes available, chunk by chunk.
            readChunkByChunk(ctx);

            if (chunk instanceof LastHttpContent) {
                readChunkByChunk(ctx);
                try {
                    prepareResponse(ctx);
                } catch (Exception e){
                   // Error handler
                }
                resetPostRequestDecoder();
            }
        }
    } else {
        // Error handler
    }
}
private void readChunkByChunk(ChannelHandlerContext ctx) {
    try {
        while (decoder.hasNext()) {
            InterfaceHttpData data = decoder.next();
            if (data != null) {
                try {
                    processChunk(ctx, data);
                } catch (IOException e) {
                    // Error handler
                } finally {
                    data.release();
                }
            }
        }
    } catch (EndOfDataDecoderException e) {
        // No more data to decode, that's fine
    }
}
private void processChunk(ChannelHandlerContext ctx, InterfaceHttpData data) throws IOException {
    LOGGER.debug("HTTP Data Name: {}, Type: {}" + data.getName() + data.getHttpDataType());

    switch (data.getHttpDataType()) {
        case Attribute:
            Attribute attrib = (Attribute) data;
            try {
                int bytes = attrib.getByteBuf().readableBytes();
                String name = attrib.getName();
                readData = attrib.getByteBuf().toString(CharsetUtil.UTF_8);
                // Attribute Handling
            } catch (IOException e) {
                // Error handler
            }
            break;
        case FileUpload:
        // FileUpload stuff

现在,表单的所有字段都会通过processChunk上的“Attribute”大小写,所以我猜测是否可以从表单中过滤提交字段的类型。

谢谢, 克里斯。

1 个答案:

答案 0 :(得分:1)

我认为客户端在根据表单发送请求时不会将类型传递给服务器。所以没有办法知道type = text,submit或file ...除了文件,因为有一个特殊的处理......

Look at w3 example at the end here