java 通过 Jersey 和 x-www-form-urlencoded 发送文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10687886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send file via Jersey and x-www-form-urlencoded
提问by deyo.vuk
I'm trying to invoke REST service with the following client code with the idea to send some String message details as well as the attachment file:
我正在尝试使用以下客户端代码调用 REST 服务,以发送一些字符串消息详细信息以及附件文件:
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 I'm targeting accepts Base64 encoded content, so that is why the additional transferring from File to ByteArray.
我的目标服务器接受 Base64 编码的内容,所以这就是从 File 到 ByteArray 的额外传输的原因。
Also, I have found that class com.sun.jersey.core.impl.provider.entity.FormProvider is noted with both for production and consuming of "x-www-form-urlencoded" requests.
此外,我发现类 com.sun.jersey.core.impl.provider.entity.FormProvider 被注意到用于“x-www-form-urlencoded”请求的生产和消费。
@Produces({"application/x-www-form-urlencoded", "*/*"})
@Consumes({"application/x-www-form-urlencoded", "*/*"})
But still I end-up with the following stacktrace:
但我仍然得到以下堆栈跟踪:
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.access0(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 one?
对这个有帮助吗?
回答by deyo.vuk
I managed to get the thing working on the client side. The problem was that I was forcing sending of file as separate message body part, while x-www-form-urlencoded is actually packing all of the data as parameters in the query that is the entire body.
我设法让这件事在客户端工作。问题是我强制将文件作为单独的消息正文部分发送,而x-www-form-urlencoded 实际上将所有数据打包为查询中的参数,即整个 body。
So the working client code if you want to send attachment via Jersey post method would be:
因此,如果您想通过 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);
The Apache Commons' Base64 encoder was required in my case to transform file into encoded byte array, not sure if this is general requirement.
在我的情况下,需要 Apache Commons 的 Base64 编码器将文件转换为编码的字节数组,不确定这是否是一般要求。
回答by Alex Stybaev
try using Multipart/form-data
instead of application/x-www-form-urlencoded
. This tutorialmight help.
尝试使用Multipart/form-data
代替application/x-www-form-urlencoded
. 本教程可能会有所帮助。