MultipartBody 取出key,value数据,打印参数

MultipartBody 打印参数比较麻烦

kotlin:

 if (requestBody is MultipartBody) {
            val params = mutableMapOf<String, String>()
            val files = mutableMapOf<String, String>()
            requestBody.parts().forEach {
                val body = it.body()
                it.headers()?.let {
                    val header = it.value(0)
                    val split = header.replace(" ", "").replace("\"", "").split(";")
                    when (split.size) {
                        2 -> {
                            //文本参数
                            val keys = split[1].split("=")
                            if (keys.size > 1 && body.contentLength() < 1024) {
                                val key = keys[1]
                                val buffer = Buffer()
                                body.writeTo(buffer)
                                val value = buffer.readUtf8()
                                params[key] = value
                            }
                        }
                        3 -> {
                            //文件
                            val fileKeys = split[1].split("=")
                            val fileKey = if (fileKeys.size > 1) {
                                fileKeys[1]
                            } else ""
                            val nameValue = split[2].split("=")
                            val fileName = if (nameValue.size > 1) nameValue[1] else ""
                            files[fileKey] = fileName
                        }
                    }
                }
            }
            println("文件-->$files")
            println("文本-->$params")
        }

java写法 


        if (requestBody instanceof MultipartBody) {
            MultipartBody body = (MultipartBody) requestBody;
            Map<String, String> params = new HashMap<>();
            Map<String, String> files = new HashMap<>();
            for (MultipartBody.Part part : body.parts()) {
                RequestBody body1 = part.body();
                Headers headers = part.headers();
                if (headers != null && headers.size() > 0) {
                    String[] split = headers.value(0).replace(" ", "").replace("\"", "").split(";");
                    if (split.length == 2) {
                        //文本
                        String[] keys = split[1].split("=");
                        if (keys.length > 1 && body1.contentLength() < 1024) {
                            String key = keys[1];
                            String value = "";
                            Buffer buffer = new Buffer();
                            body.writeTo(buffer);
                            value = buffer.readUtf8();
                            params.put(key, value);
                        }
                    } else if (split.length == 3) {
                        //文件
                        String fileKey = "";
                        String fileName = "";
                        String[] keys = split[1].split("=");
                        String[] names = split[2].split("=");
                        if (keys.length > 1) fileKey = keys[1];
                        if (names.length > 1) fileName = names[1];
                        files.put(fileKey, fileName);
                    }
                }

            }
            System.out.println("文本参数-->" + params);
            System.out.println("文件参数-->" + files);
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值