簡體   English   中英

Apache Http Client 4 Form Post多部分數據

[英]Apache Http Client 4 Form Post Multi-part data

我需要通過HTTP請求(其中一個是文件)將一些表單參數發布到服務器。 所以我像這樣使用Apache HTTP Client ...

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)

我從其他帖子中了解到,我需要以某種方式提出一個邊界,這是一個在內容中找不到的字符串。 但是我如何在上面的代碼中創建這個邊界? 它應該是另一個參數嗎? 只需一個代碼示例就是我需要的。

例外情況說,您沒有指定“多部分邊界”。 這是一個字符串,充當請求中不同部分之間的分隔符。 但在你的情況下,似乎你沒有處理任何不同的部分。

您可能想要使用的是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通常不用於multipart,它默認為content-type application/x-www-form-urlencoded

我接受了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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM