简体   繁体   English

Apache Http Client 4 Form Post多部分数据

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

I need to post some form parameters to a server through an HTTP request (one of which is a file). 我需要通过HTTP请求(其中一个是文件)将一些表单参数发布到服务器。 So I use Apache HTTP Client like so... 所以我像这样使用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 server returns an error, stack trace is.. 服务器返回错误,堆栈跟踪是..

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)

I understand from other posts that I need to somehow come up with a boundary, which is a string not found in the content. 我从其他帖子中了解到,我需要以某种方式提出一个边界,这是一个在内容中找不到的字符串。 But how do I create this boundary in the code I have above? 但是我如何在上面的代码中创建这个边界? Should it be another parameter? 它应该是另一个参数吗? Just a code sample is what I need. 只需一个代码示例就是我需要的。

As the exception says, you have not specified the "multipart boundary". 例外情况说,您没有指定“多部分边界”。 This is a string that acts as a separator between the different parts in the request. 这是一个字符串,充当请求中不同部分之间的分隔符。 But in you case it seems like you do not handle any different parts. 但在你的情况下,似乎你没有处理任何不同的部分。

What you probably want to use is MultipartEntityBuilder so you don't have to worry about how it all works under the hood. 您可能想要使用的是MultipartEntityBuilder,因此您不必担心它是如何工作的。

It should be Ok to do the following 应该可以做以下事情

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

However, here is a version that should be compatible with @AbuMariam findings below but without the use of deprecated methods/constructors. 但是,这里的版本应与下面的@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);

The UrlEncodedFormEntity is normally not used for multipart, and it defaults to content-type application/x-www-form-urlencoded UrlEncodedFormEntity通常不用于multipart,它默认为content-type application/x-www-form-urlencoded

I accepted gustf's answer because it got rid of the exception I was having and so I thought I was on the right track, but it was not complete. 我接受了gustf的回答,因为它摆脱了我所遇到的异常,因此我认为我在正确的轨道上,但它并不完整。 The below is what I did to finally get it to work... 以下是我最终让它工作的所作所为......

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