【问题标题】:Form file uploads in jersey-client 1.18在 jersey-client 1.18 中上传表单文件
【发布时间】:2016-12-25 19:52:19
【问题描述】:

我正在尝试使用 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 条回复。

很遗憾,由于我无法控制的情况,无法切换到其他版本。任何帮助将不胜感激!

【问题讨论】:

    标签: java jersey jersey-client jersey-1.0


    【解决方案1】:

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

    Jersey REST Client : Posting MultiPart data

    【讨论】:

    • 谢谢。不知道我在搜索时是怎么错过的。
    • 您好,您可以粘贴您的代码吗?我也收到错误 400
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    相关资源
    最近更新 更多