Apache Http Client 4 Form Post多部分数据

Abu*_*iam 4 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);
Run Code Online (Sandbox Code Playgroud)

服务器返回错误,堆栈跟踪是..

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)
Run Code Online (Sandbox Code Playgroud)

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

gus*_*stf 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);
Run Code Online (Sandbox Code Playgroud)

但是,这里的版本应与下面的@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);
Run Code Online (Sandbox Code Playgroud)

UrlEncodedFormEntity是通常不用于多部分,并且它默认为内容类型application/x-www-form-urlencoded


Abu*_*iam 2

我接受了古斯特夫的回答,因为它消除了我遇到的异常,所以我认为我走在正确的轨道上,但它并不完整。以下是我最终让它发挥作用所做的事情......

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);
Run Code Online (Sandbox Code Playgroud)