Netty获取 post请求 body里面二进制流

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);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值