gpt4 book ai didi

java - Netty POST 表单输入类型

转载 作者:行者123 更新时间:2023-12-01 11:24:49 25 4
gpt4 key购买 nike

我在从 netty 收到的 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 属性。

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

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”案例,所以我猜测是否可以获取它们的类型来过滤表单中的提交字段。

谢谢,克里斯。

最佳答案

我相信当客户端发送基于表单的请求时,类型不会传递到服务器。所以没有办法知道 type=text、submit 或 file...除了 file 因为有特殊处理...

Look at w3 example at the end here .

关于java - Netty POST 表单输入类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30911717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com