Apache Http Client 4 Form Post 多部分数据

Apache Http Client 4 Form Post Multi-part data

我需要通过 HTTP 请求(其中一个是文件)向服务器 post 发送一些表单参数。所以我像这样使用 Apache HTTP 客户端...

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);

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

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)

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

如异常所述,您没有指定“多部分边界”。这是一个字符串,用作请求中不同部分之间的分隔符。但在你的情况下,你似乎没有处理任何不同的部分。

您可能想要使用的是 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 发现兼容,但不使用已弃用的 methods/constructors。

        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-typeapplication/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);