Java 多部分帖子

标签 java multipartform-data

我正在尝试使用以下正文创建一个指向 URL 的多部分帖子:

Content-Disposition: form-data; name="json"
Content-Type: "application/json; charset=UTF-8"

{"input1":"data1","input2":"data2","input3":"data3"}

--APIMultipartPost
Content-Disposition: form-data; name="filePath"; filename="myFile.dat"
Content-Length: 381645
Content-Type: text/plain
Content-Transfer-Encoding: binary

<!-- SNIP -->
<!-- The OWL file was included here in plain text (without the SNIPs) -->
<!-- SNIP -->

我尝试使用 MultipartEntityBuilder 创建多部分帖子,但可能我的参数出了问题。 有人可以帮我写这篇文章的 java 代码吗?

这是我的代码:

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost(URL);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("input1", "data1");
builder.addTextBody("input2", "data2");
builder.addTextBody("input3", "data3");

builder.addBinaryBody("file", new File("C:/myFile.dat"), ContentType.APPLICATION_OCTET_STREAM, "myFile.dat");
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
HttpResponse response = httpClient.execute(uploadFile);

以及我得到的错误:

HttpResponseProxy{HTTP/1.1 422 Unprocessable Entity [Server: nginx/1.6.0, Date: Wed, 28 Jan 2015 19:29:42 GMT, Content-Type: application/json;charset=utf-8, Content-Length: 89, Connection: keep-alive, Status: 422 Unprocessable Entity, X-Rack-Cache: invalidate, pass, X-Content-Type-Options: nosniff] ResponseEntityProxy{[Content-Type: application/json;charset=utf-8,Content-Length: 89,Chunked: false]}}

谢谢!

最佳答案

响应 422 Unprocessable Entity 说

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

如您所见,您发送的请求不符合您发布到的 Controller /服务的规范。如果您使用像 TCPMon 这样的工具您实际上可以拦截您发送的请求并检查它的样子。当我使用你的代码执行此操作时,它看起来像

POST /url HTTP/1.1
Content-Length: 739
Content-Type: multipart/form-data; boundary=L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c
Host: 127.0.0.1:8090
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.3.6 (java 1.5)
Accept-Encoding: gzip,deflate

--L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c
Content-Disposition: form-data; name="input1"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

data1
--L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c
Content-Disposition: form-data; name="input2"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

data2
--L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c
Content-Disposition: form-data; name="input3"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

data3
--L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c
Content-Disposition: form-data; name="file"; filename="myFile.dat"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary


--L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c--

这不是你想要的。你只需要一个包含两部分的请求,一是json数据,二是文件。

因此,如果您将代码更改如下。

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost("http://localhost:8090/1");

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
String jsonStr = "{\"input1\":\"data1\",\"input2\":\"data2\",\"input3\":\"data3\"}";
builder.addTextBody("json", jsonStr, ContentType.APPLICATION_JSON);
builder.addBinaryBody("file", new File("/path/to/file"),
        ContentType.TEXT_PLAIN, "myFile.dat");

HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
HttpResponse response = httpClient.execute(uploadFile);

您可以提供如下请求。

POST /url HTTP/1.1
Content-Length: 468
Content-Type: multipart/form-data; boundary=mN_bWsS4QQnlPJksvinB_WUpl2Qi6zVVElUEEBKh
Host: 127.0.0.1:8090
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.3.6 (java 1.5)
Accept-Encoding: gzip,deflate

--mN_bWsS4QQnlPJksvinB_WUpl2Qi6zVVElUEEBKh
Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: 8bit

{"input1":"data1","input2":"data2","input3":"data3"}
--mN_bWsS4QQnlPJksvinB_WUpl2Qi6zVVElUEEBKh
Content-Disposition: form-data; name="file"; filename="myFile.dat"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: binary


--mN_bWsS4QQnlPJksvinB_WUpl2Qi6zVVElUEEBKh--

这就是您所需要的。

关于Java 多部分帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28197083/

相关文章:

javascript - 正确读取多部分数据流(NodeJS)

javascript - FormData 将 bool 值作为字符串发送到服务器

java - 设计 FIX 消息编码器和解码器

java - 如果它是 Java 中的整数数组,如何检查 null 元素?

java - 安卓.content.res.Resources$NotFoundException : Unable to find resource ID

ios - AFNetworking V 2 是否支持非流式多部分后台上传任务?

javascript - 如何使用nodejs实现分段文件上传代理?

java - 什么时候将 Activity 传递给另一个类会导致内存泄漏?

java - 在我的屏幕上放置重新启动按钮

ios - 使用 JSON 中的 NSData 上传多部分图像(HTTP 正文)