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

Form file uploads in jersey-client 1.18

我正在尝试使用 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

到目前为止我已经尝试过(修改路径和 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);

但我只收到了 400 的响应。

不幸的是,由于我无法控制的情况,无法切换到不同的版本。如有任何帮助,我们将不胜感激!

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