2016-03-30 117 views
4

我需要通过HTTP请求(其中之一是文件)将某些表单参数发布到服务器。所以我使用Apache HTTP客户端是这样的...Apache Http客户端4表单发布多部分数据

HttpPost httpPost = new HttpPost(urlStr); 

params = [] 
params.add(new BasicNameValuePair("username", "bond")); 
params.add(new BasicNameValuePair("password", "vesper")); 
params.add(new BasicNameValuePair("file", payload)); 

httpPost.setEntity(new UrlEncodedFormEntity(params)); 
httpPost.setHeader("Content-type", "multipart/form-data"); 

CloseableHttpResponse response = httpclient.execute(httpPost); 

服务器返回一个错误,堆栈跟踪是..

the request was rejected because no multipart boundary was found 
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:954) 
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331) 
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:351) 
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126) 
at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:156) 

我从我需要以某种方式拿出其他职位了解边界,这是在内容中找不到的字符串。但是,我如何在上面的代码中创建这个边界?它应该是另一个参数吗?只需要一个代码示例即可。

回答

1

我接受gustf的答案,因为它摆脱了我是有例外的,所以我认为我是在正确的轨道,但它并不完整。下面是我最后得到它的工作...

File payload = new File("/Users/CasinoRoyaleBank") 
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
entity.addPart("file", new FileBody(payload)) 
entity.addPart("username", new StringBody("bond")) 
entity.addPart("password", new StringBody("vesper")) 
httpPost.setEntity(entity); 
CloseableHttpResponse response = httpclient.execute(httpPost); 
+0

好吧,我明白了。对不起,但是当你接受答案时,我认为它是为你工作的。我会用你的发现更新我的答案。 – gustf

+0

嗨,我现在用一个使用构建器的等效代码更新了答案。我还检查了与原始代码的不同之处:1)将addPart(“file”,new FileBody(payload)'放在其他部分之前2)字符集,它在代码中的不建议使用的构造函数中默认为“ASCII” 。而在我原来的默认是'ISO_8859_1'。这通常不应该是任何问题,所以我的想法是,它是第一个也是奇怪的。但是也许这就是'MultipartHttpServletRequest'的工作方式,我不知道。如果你有兴趣和时间来测试它,那将会很棒。 – gustf

8

正如例外所述,您尚未指定“多部分边界”。这是一个字符串,充当请求中不同部分之间的分隔符。但在你看来,你似乎没有处理任何不同的部分。

你可能想要使用的是MultipartEntityBuilder,所以你不必担心它是如何工作的。

应该没做到以下几点

 HttpPost httpPost = new HttpPost(urlStr); 

     File payload = new File("/Users/CasinoRoyaleBank"); 

     HttpEntity entity = MultipartEntityBuilder.create() 
       .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) 
       .addBinaryBody("file", payload) 
       .addTextBody("username", "bond") 
       .addTextBody("password", "vesper") 
       .build(); 
     httpPost.setEntity(entity); 

然而,这里应该是下面,但没有使用过时的方法/构造的@AbuMariam发现兼容的版本。

 File payload = new File("/Users/CasinoRoyaleBank"); 

     ContentType plainAsciiContentType = ContentType.create("text/plain", Consts.ASCII); 
     HttpEntity entity = MultipartEntityBuilder.create() 
       .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) 
       .addPart("file", new FileBody(payload)) 
       .addPart("username", new StringBody("bond", plainAsciiContentType)) 
       .addPart("password", new StringBody("vesper", plainAsciiContentType)) 
       .build(); 
     httpPost.setEntity(entity); 

     CloseableHttpResponse response = httpclient.execute(httpPost); 

UrlEncodedFormEntity通常不用于多,其默认值为内容类型application/x-www-form-urlencoded

+0

您的建议工作,我不再有错误。但在服务器端,当我读取这些数据时,我正在做... MultipartHttpServletRequest multiRequest =(MultipartHttpServletRequest)请求; CommonsMultipartFile file =(CommonsMultipartFile)multiRequest.getFile(“file”);和文件出来为空。 – AbuMariam

+0

更新了'builder.addBinaryBody(String,File)'的答案。但我想你已经看到了这一个:) – gustf

+1

我不知道为什么这没有更多的提升,寻找2小时后,很多复杂的解决方案,这很简单,像一个魅力。在stackoverflow上这个主题的最佳答案。 –