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

时间:2016-08-18 19:02:50

标签: java jersey jersey-client jersey-1.0

我正在尝试使用jersey-client 1.18将文件上传到远程API,但我一直无法弄清楚如何。基本上,我想要做的是等同于以下命令:

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

但我无法辨别使用jersey-client 1.x将文件作为multipart / form-data上传的方法。

到目前为止,我已尝试过(修改路径和网址以保护内疚):

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);

但我只收到了400个回复。

不幸的是,由于我无法控制的情况,切换到其他版本不是一种选择。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我认为这已经得到了解答。您应该使用FormDataMultiPart

Jersey REST Client : Posting MultiPart data