解决httpclient上传multipart form-data文件失败

1 篇文章 0 订阅

目标URL的multipart form-data是这样的

-----------------------------7e1a51350702
Content-Disposition: form-data; name="keys"


-----------------------------7e1a51350702
Content-Disposition: form-data; name="typeid"

1
-----------------------------7e1a51350702
Content-Disposition: form-data; name="BR_DESCRIPTION"


-----------------------------7e1a51350702
Content-Disposition: form-data; name="BR_EXPIREDATE"


-----------------------------7e1a51350702
Content-Disposition: form-data; name="BATCH_IMPORTFILE"; filename="xxx.txt"
Content-Type: text/plain

11111111111

-----------------------------7e1a51350702
Content-Disposition: form-data; name="BATCH_TYPE"

0
-----------------------------7e1a51350702--

然后原来的代码是这样的

            File uploadFile=new File(pre+"xxx.txt");
            ContentType contentType=ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), "GBK");
            FileBody fileBody=new FileBody(uploadFile, contentType,"xxx.txt");
            StringBody keys=new StringBody("",ContentType.MULTIPART_FORM_DATA);
            StringBody typeid=new StringBody("3", ContentType.MULTIPART_FORM_DATA);
            StringBody BR_DESCRIPTION=new StringBody("", ContentType.MULTIPART_FORM_DATA);
            StringBody BR_EXPIREDATE=new StringBody("", ContentType.MULTIPART_FORM_DATA);
            StringBody BATCH_TYPE=new StringBody("0", ContentType.MULTIPART_FORM_DATA);
            HttpEntity httpEntity= MultipartEntityBuilder.create()
                    .setLaxMode()
                    .addPart("keys",keys)
                    .addPart("typeid", typeid)
                    .addPart("BR_DESCRIPTION", BR_DESCRIPTION)
                    .addPart("BR_EXPIREDATE", BR_EXPIREDATE)
                    .addPart("BATCH_IMPORTFILE", fileBody)
                    .addPart("BATCH_TYPE", BATCH_TYPE)
                    .build();
            HttpPost doImportAdd=new HttpPost(importList);
            doImportAdd.setEntity(httpEntity);
            doImportAdd.addHeader("Accept", "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
            doImportAdd.setHeader("Accept-Language", "zh-CN");
            doImportAdd.setHeader("User-Agent", " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; MDDS; InfoPath.2)");
            doImportAdd.setHeader("Content-Type", "multipart/form-data");
            doImportAdd.setHeader("boundary", "---------------------------7e1103115079a");
            doImportAdd.setHeader("Accept-Encoding", "gzip, deflate");
            doImportAdd.setHeader("Connection", "Keep-Alive");

结果总是返回数据添加失败,但是返回的状态码是200,使用wireshark抓包,由于是使用程序执行的,所以选择本地连接,筛选出HTTP的请求
结果发现提示the multipart dissector could not find a required parameter

搜索该问题后在Stack Overflow 中找到有关的解决方法,就是把所有Header的构造都删除

删除后

 File uploadFile=new File(pre+"xxx.txt");
            ContentType contentType=ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), "GBK");
            FileBody fileBody=new FileBody(uploadFile, contentType,"xxx.txt");
            StringBody keys=new StringBody("",ContentType.MULTIPART_FORM_DATA);
            StringBody typeid=new StringBody("3", ContentType.MULTIPART_FORM_DATA);
            StringBody BR_DESCRIPTION=new StringBody("", ContentType.MULTIPART_FORM_DATA);
            StringBody BR_EXPIREDATE=new StringBody("", ContentType.MULTIPART_FORM_DATA);
            StringBody BATCH_TYPE=new StringBody("0", ContentType.MULTIPART_FORM_DATA);
            HttpEntity httpEntity= MultipartEntityBuilder.create()
                    .setLaxMode()
                    .addPart("keys",keys)
                    .addPart("typeid", typeid)
                    .addPart("BR_DESCRIPTION", BR_DESCRIPTION)
                    .addPart("BR_EXPIREDATE", BR_EXPIREDATE)
                    .addPart("BATCH_IMPORTFILE", fileBody)
                    .addPart("BATCH_TYPE", BATCH_TYPE)
                    .build();
            HttpPost doImportAdd=new HttpPost(importList);
            doImportAdd.setEntity(httpEntity);

结果成功上传文本文件。

个人理解是因为在构建MultipartEntityBuilder是已经将模式设置为HttpMultipartMode.BROWSER_COMPATIBLE,在构建Header的话可能会产生冲突。只是个人见解,不知道是否正确。欢迎纠正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值