Submit file via Jersey and x-www-form-urlencoded

I am trying to call a REST service with the following client code with the idea of ​​sending some String message data as well as an attachment file:

ClientConfig config = new DefaultClientConfig();
config.getClasses().add(FormProvider.class);
Client client = Client.create(config);
WebResource webResource = client.resource("http://some.url/path1/path2");

File attachment = new File("./file.zip");

FormDataBodyPart fdp = new FormDataBodyPart(
            "content", 
            new ByteArrayInputStream(Base64.encode(FileUtils.readFileToByteArray(attachedLogs))),
            MediaType.APPLICATION_OCTET_STREAM_TYPE);
form.bodyPart(fdp);

ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, form);     

The server on which I am targeting accepts Base64 encoded content, so this requires additional transfer from the file to ByteArray.

In addition, I found that the class com.sun.jersey.core.impl.provider.entity.FormProvider is marked for both production and consumption of x-www-form-urlencoded requests.

@Produces({"application/x-www-form-urlencoded", "*/*"})
@Consumes({"application/x-www-form-urlencoded", "*/*"})

But still I get the following stack:

com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.multipart.FormDataMultiPart, and MIME media type, application/x-www-form-urlencoded, was not found at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149) ~[jersey-client-1.9.1.jar:1.9.1]
at com.sun.jersey.api.client.Client.handle(Client.java:648) ~[jersey-client-1.9.1.jar:1.9.1]
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670) ~[jersey-client-1.9.1.jar:1.9.1]
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.9.1.jar:1.9.1]
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:563) ~[jersey-client-1.9.1.jar:1.9.1]

Any help on this?

+3
source share
2 answers

, . , , x-www-form-urlencoded - .

, , Jersey post, :

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource webResource = client.resource("http://some.url/path1/path2");

MultivaluedMapImpl values = new MultivaluedMapImpl();
values.add("filename", "report.zip");
values.add("text", "Test message");
values.add("content", new String(Base64.encode(FileUtils.readFileToByteArray(attachedLogs))));
ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, values);

Base64 Apache Commons , , .

+4

Multipart/form-data application/x-www-form-urlencoded. .

+1

All Articles