简体   繁体   English

在jersey-client 1.18中上传表单文件

[英]Form file uploads in jersey-client 1.18

I'm trying to upload a file to a remote API using jersey-client 1.18, but I've been unable to figure out how. 我正在尝试使用jersey-client 1.18将文件上传到远程API,但是我一直无法弄清楚该怎么做。 Essentially, what I want to do is equivalent to the following command: 本质上,我想做的事情等效于以下命令:

curl -sv "http://localhost:4567/api/image" -X POST -F "file=@test2.png" -F "description=a%20b%20dce"

But I can't discern any way to upload a file as multipart/form-data using jersey-client 1.x. 但是我无法辨别任何使用jersey-client 1.x将文件作为多部分/表单数据上传的方法。

So far I've tried (paths and URLs modified to protect the guilty): 到目前为止,我已经尝试过(修改路径和URL以保护罪恶感):

FileInputStream fileInputStream = new FileInputStream(new File("test2.png"));
Form form = new Form();
form.add("description", "a b dce");
form.add("file", IOUtils.toString(fileInputStream, "UTF-8"));

client.resource("http://localhost:4567/api/image")
  .header("content-type", MediaType.MULTIPART_FORM_DATA)
  .post(ClientResponse.class, form);

But I've only received 400s in response. 但是我只收到400s的回复。

Unfortunately, switching to a different version is not an option, due to circumstances outside of my control. 不幸的是,由于我无法控制的情况,切换到其他版本是不可行的。 Any help would be appreciated! 任何帮助,将不胜感激!

I think this has already been answered. 我认为这已经得到了回答。 You should be using FormDataMultiPart 您应该使用FormDataMultiPart

Jersey REST Client : Posting MultiPart data Jersey REST客户端:发布MultiPart数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM