目标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的话可能会产生冲突。只是个人见解,不知道是否正确。欢迎纠正。