NutzCN Logo
问答 关于multipart/form-data请求的处理问题
发布于 2224天前 作者 萧仁武 2560 次浏览 复制 上一个帖子 下一个帖子
标签:

有这么一个应用场景:
对接支付接口,发送下单请求参数,获取同步响应中的支付链接实际支付之后,上游会根据下单请求参数中的notify_url地址发送支付成功的异步回调通知,现在遇到一个发送过来的回调信息是 form表单属性enctype为“multipart/form-data”的,用getParameter()方法取不到值,请问应该怎么解析这种请求发过来的回调数据?

4 回复
@AdaptBy(type=WhaleAdaptor.class)

或者当文件上传用UploadAdaptor

添加之后,后台打印

[DEBUG] 2018-10-20 13:30:43.258 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:54) - info created
[DEBUG] 2018-10-20 13:30:43.258 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:60) - Params map created - 0 params
[DEBUG] 2018-10-20 13:30:43.259 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:77) - boundary:
----------------------------660444145968326728217114
[DEBUG] 2018-10-20 13:30:43.262 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:100) - skip first boundary
[DEBUG] 2018-10-20 13:30:43.262 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) - Reading...
[DEBUG] 2018-10-20 13:30:43.263 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[null],fieldName=[status]
[DEBUG] 2018-10-20 13:30:43.264 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) - Found a param, name=[status] value=[1]
[DEBUG] 2018-10-20 13:30:43.264 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[null],fieldName=[partner]
[DEBUG] 2018-10-20 13:30:43.264 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) - Found a param, name=[partner] value=[1396]
[DEBUG] 2018-10-20 13:30:43.264 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[null],fieldName=[sdpayno]
[DEBUG] 2018-10-20 13:30:43.265 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) - Found a param, name=[sdpayno] value=[m2018102009194712740]
[DEBUG] 2018-10-20 13:30:43.265 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[null],fieldName=[sdorderno]
[DEBUG] 2018-10-20 13:30:43.265 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) - Found a param, name=[sdorderno] value=[1539998363202]
[DEBUG] 2018-10-20 13:30:43.265 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[null],fieldName=[paymoney]
[DEBUG] 2018-10-20 13:30:43.266 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) - Found a param, name=[paymoney] value=[0.01]
[DEBUG] 2018-10-20 13:30:43.266 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[null],fieldName=[paytype]
[DEBUG] 2018-10-20 13:30:43.266 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) - Found a param, name=[paytype] value=[weixin]
[DEBUG] 2018-10-20 13:30:43.267 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[null],fieldName=[remark]
[DEBUG] 2018-10-20 13:30:43.267 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) - Found a param, name=[remark] value=[]
[DEBUG] 2018-10-20 13:30:43.267 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[null],fieldName=[sign]
[DEBUG] 2018-10-20 13:30:43.267 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) - Found a param, name=[sign] value=[5080bc6b6f0e017bbab82efd3e4f96bc]
[DEBUG] 2018-10-20 13:30:43.268 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:231) - ...Done 982 bytes readed

我这时候,除了用 @Param("status") String status 形参注解的形式获取字段对应的值之外,是否方法直接从 HttpServletRequest 中获取值,因为发来的字段数量和名称是不确定的,需要转一下存到map里

找到解决方法了,后台代码如下

 @At("/notify")
    @Ok("raw")
    @AdaptBy(type = WhaleAdaptor.class)
    public String notify(@Param("..") Map map) throws IOException {
        log.debug("map-------:" + Json.toJson(map, JsonFormat.full()));
        return Json.toJson(map);
    }

添加了@AdaptBy(type = WhaleAdaptor.class)之后,形参用一个map接收一下,直接就是请求中的键值对

添加回复
请先登陆
回到顶部