Apache Http Client 4表单发布多部分数据

时间:2016-03-30 19:29:22

标签: java groovy multipartform-data apache-httpclient-4.x

我需要通过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)

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

2 个答案:

答案 0 :(得分:9)

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

你可能想要使用的是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

答案 1 :(得分:2)

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