1 from data方式提交数据 读取方式
废话不多说 直接上代码
private HttpPostRequestDecoder decoder;//Handel私有变量
HttpRequest request = (HttpRequest) msg;//第一次连接建立 msg传过来的是HttpRequest
decoder = new HttpPostRequestDecoder(factory, request);//实例化一个HttpPostRequestDecoder
//第二次连接 post请求传输的是数据 msg传过来的是 HttpContent
HttpContent chunk = (HttpContent) msg;
if (decoder != null) { try { decoder.offer(chunk); } catch (HttpPostRequestDecoder.ErrorDataDecoderException e1) { e1.printStackTrace(); writeResponse(ctx.channel(), e1.getMessage()); } try { //遍历 while (decoder.hasNext()) { InterfaceHttpData data = decoder.next(); if (data != null) { try { writeHttpData(data); } finally { data.release(); } } } } catch (HttpPostRequestDecoder.EndOfDataDecoderException e1) { } }
//真正读取post中 fromdata数据的操作
private void writeHttpData(InterfaceHttpData data) { // System.out.println("InterfaceHttpData "+data.getHttpDataType()); // System.out.println("InterfaceHttpData ->"+data.getName()); /** * HttpDataType有三种类型 * Attribute, FileUpload, InternalAttribute */ if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) { Attribute attribute = (Attribute) data; String value; try { value = attribute.getValue(); requestData.put(attribute.getName(), value); } catch (IOException e1) { e1.printStackTrace(); return; } } }
2 二进制流数据 获取方式 (本人在项目中 用于微信小程序支付成功的回调)
ByteBuf buf = chunk.content();//每一次传输 都获取bytebuf String s = buf.toString(CharsetUtil.UTF_8); //转码 if(!requestData.containsKey("content")) {//对每一次的数据传输 进行存储 requestData.put("content", s); }else { String con = requestData.get("content"); con=con+s; requestData.put("content",con); }