2016-03-30 119 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上這個主題的最佳答案。 –