上传PNG文件导致状态406不可接受

时间:2017-04-12 16:40:58

标签: jersey jersey-client

我在这里难过。我正在开发一个测试,将PNG文件上传到REST服务,并继续获得406 Not Acceptable作为响应。

我正在使用jersey-bundle 1.19.3

这是请求:

Apr 12, 2017 12:20:17 PM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client out-bound request
1 > POST http://localhost:7001/api/1/payments/XT491926022464/attachments
1 > Accept: application/xml
1 > Content-Type: multipart/form-data; boundary=---------------------------1492014017228
1 > Api-Key: 2222
1 > Authorization: Bearer 1152921504606857464
-----------------------------1492014017228
Content-Type: text/plain
Content-Disposition: form-data; name="requestId"

1234
-----------------------------1492014017228
Content-Type: text/plain
Content-Disposition: form-data; name="file"

BankCustomerAttachmentTestImage.PNG
-----------------------------1492014017228
Content-Type: text/plain
Content-Disposition: form-data; name="description"

TEST
-----------------------------1492014017228
Content-Type: application/octet-stream
Content-Disposition: form-data; filename="junit8251833935221639216.tmp";   name="file"

...

以下是回复:

Apr 12, 2017 12:20:17 PM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client in-bound response
1 < 406
1 < Access-Control-Allow-Headers: Content-Type
1 < Access-Control-Allow-Origin: *
1 < Date: Wed, 12 Apr 2017 16:20:17 GMT
1 < Content-Length: 14
1 < Access-Control-Allow-Methods: GET, POST, DELETE, PUT
1 < Content-Type: text/html; charset=UTF-8
1 < Connection: close
1 < X-Powered-By: Servlet/3.0 JSP/2.2
1 < 
Not Acceptable

这是代码......我做错了什么?

    DefaultClientConfig config = new DefaultClientConfig();
    Client restClient = Client.create(config);
    restClient.addFilter(new LoggingFilter());

    WebResource restWebResource = restClient.resource("http://...");
    WebResource.Builder webResourceBuilder = restWebResource.accept(MediaType.APPLICATION_XML);

webResourceBuilder.type(MediaType.MULTIPART_FORM_DATA);
webResourceBuilder.header(RESTActor.HEADER_API_KEY, "1234");
webResourceBuilder.header(HttpHeaders.AUTHORIZATION, "Bearer " + "1152921504606857464");

FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", fileObject, MediaType.APPLICATION_OCTET_STREAM_TYPE);

    fileDataBodyPart.setContentDisposition(
    FormDataContentDisposition.name("file").fileName(fileObject.getName()).build());

    FormDataMultiPart multiPartForm = new FormDataMultiPart();
    multiPartForm.field("requestId", "1234");
    multiPartForm.field("file", "BankCustomerAttachmentTestImage.PNG");
    multiPartForm.field("description", "TEST");

    multiPartForm.bodyPart(fileDataBodyPart);
    multiPartForm.setMediaType(MULTIPART_FORM_DATA_TYPE);

    final String boundary = "---------------------------" + System.currentTimeMillis();

    ClientResponse currentRestClientResponse =
 webResourceBuilder.type(MediaType.MULTIPART_FORM_DATA + "; boundary=" + boundary).post(ClientResponse.class, multiPartForm);

1 个答案:

答案 0 :(得分:1)

在您的请求中,您指明客户端仅接受application / xml content-type:

1 > Accept: application/xml

但是服务器无法返回此内容类型,这就是406 Not Acceptable错误的含义。 您的客户端必须接受服务器返回的内容类型,即:

1 < Content-Type: text/html; charset=UTF-8

为此,请将以下行中的MediaType更改为MediaType.TEXT_HTML:

    WebResource.Builder webResourceBuilder = restWebResource.accept(MediaType.TEXT_HTML);

然后检查真实的服务器响应。可能会发现服务器端存在另一个问题,例如:它使用HTTP 500状态代码进行响应,一旦您解决它,您将需要将接受的媒体类型调整为某些特定的。