我正在尝试通过我的java代码发送多部分formdata帖子。谁能告诉我如何在下面设置内容长度?当我们使用实现ContentDescriptor接口的InputStreamBody时,似乎涉及到头文件。在我添加内容后,在InputStreamBody上执行getContentLength会给出-1。我将它子类化为contentLength我的字节数组的长度,但不确定ContentDescriptor所需的其他头是否会设置为正确的POST。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(myURL);
ContentBody cb = new InputStreamBody(new ByteArrayInputStream(bytearray), myMimeType, filename);
//ContentBody cb = new ByteArrayBody(bytearray, myMimeType, filename);
MultipartEntity mpentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
mpentity.addPart("key", new StringBody("SOME_KEY"));
mpentity.addPart("output", new StringBody("SOME_NAME"));
mpentity.addPart("content", cb);
httpPost.setEntity(mpentity);
HttpResponse response = httpclient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
答案 0 :(得分:3)
我是你注释掉的ByteArrayBody课程的作者。
我写的是因为我遇到了同样的问题。原始的Jira票证在这里:https://issues.apache.org/jira/browse/HTTPCLIENT-1014
所以,既然你已经有一个byte [],要么将HttpMime升级到包含这个类的最新版本4.1-beta1。或者将Jira问题中的代码复制到您自己的项目中。
ByteArrayBody类将完全满足您的需求。