Java HttpClient 3.1 Multipart Post

时间:2016-05-03 11:36:57

标签: java post multipartform-data apache-commons-httpclient

我正在使用Java HttpClient 3.1来编写REST API调用。我遇到multipart/form-data的邮件问题。以下是我尝试进行的API调用,以API提供商为例进行说明:

POST /v1/documents HTTP/1.1
Host: .......
x-session-key: 02e57c7d-d071-4c63-b491-1194a9939ea5.2016-01-13T22:34:53.101Z
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="documentImage"; filename="ScreenShot 2016-01-14 at 1.48.48 PM.png"
Content-Type: image/png


----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="documentType"

Picture ID
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="token"

02e57c7d-d071-4c63-b491-1194a9939ea5
----WebKitFormBoundary7MA4YWxkTrZu0gW

这是我的HttpClient 3.1代码:

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;


Part[] parts = new Part[3];
parts[0] = new StringPart("token", this.user.userID);
parts[1] = new StringPart("documentType", "Picture ID");

try {
    File imageFile = new File("/path/test2.png");
    parts[2] = new FilePart("documentImage", imageFile);
} catch (FileNotFoundException ex) {
    .....
}

PostMethod postMethod = new PostMethod(hostURL + "/documents");

MultipartRequestEntity entity = new MultipartRequestEntity(parts, postMethod.getParams());
postMethod.setRequestEntity(entity);

postMethod.addRequestHeader("Content-Type", "multipart/form-data");
postMethod.addRequestHeader("x-session-key", "sjkkjksjkkjdjk");

HttpClient httpClient = new HttpClient();
int statusCode = 0;
try {
    statusCode = httpClient.executeMethod(httpMethod);
} catch (IOException ex) {
    ....
}

String respond = null;
try {
    StringBuilder resultStr = new StringBuilder();
    try (BufferedReader rd = new BufferedReader(new InputStreamReader(httpMethod.getResponseBodyAsStream()))) {
        String line;
        while ((line = rd.readLine()) != null) {
            resultStr.append(line);
        }
    }
    respond = resultStr.toString();
    httpMethod.releaseConnection();
} catch (IOException ex) {
    .....
}

System.out.println("status code: " + statusCode + ", respond: " + respond);

回复我从API服务器得到的是:

status code: 400, respond: {"code":400,"message":"Unknown content type [contentType=application/octet-stream]"}

我已将multipart/form-data设为postMethod.addRequestHeader("Content-Type", "multipart/form-data");

我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。这是修复:

Sub A()
    Dim x

    'This works
    Set x = CreateObject("Scripting.FileSystemObject")
    Set x = Nothing

    'This throws an error 429 "Active component cannot create object."
    Set x = CreateObject("MSXML2.DOMDocument60")
    Set x = Nothing

    'The only way I can create object is to add the reference using GUID
    Dim y As MSXML2.DOMDocument60
    Set y = New MSXML2.DOMDocument60
    Set y = Nothing
    'This works like a charm

End Sub

它的作用是为FilePart设置parts[2] = new FilePart("documentImage", f, "image/png", null); 。如果未在构造函数中设置,则CONTENT_TYPEDEFAULT_CONTENT_TYPE

我使用的构造方法是:

application/octet-stream