如何将CXF用于带有附加文件的客户端REST帖子

时间:2012-05-10 00:13:56

标签: java rest cxf

我正在编写一个Java客户端,需要使用REST帖子将文件上传到服务器。我有一个与文件一起发送的信息模式,但文件本身将作为附件发送到邮件中。服务器不是用Java编写的,我也不能(轻松)访问源代码。

如何在CXF中创建帖子消息?我发现了一个similar SO question,但它似乎使用了我在CXF中找不到的特定于Jersey的类。 CXF已经在项目中使用,所以我更喜欢使用它,但如果需要,我可以使用另一个库。

如果不是很明显,这是我第一次使用REST服务。

1 个答案:

答案 0 :(得分:1)

您是否看到the bit in the Apache CXF User Guide其中WebClient与MultipartBody,附件或文件一起使用?示例代码摘录在下面无耻地复制:

WebClient client = WebClient.create("http://books");
client.type("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg");
Attachment att = new Attachment("root", imageInputStream, cd);
client.post(new MultipartBody(att));

// or just post the attachment if it's a single part request only
client.post(att);

// or just use a file
client.post(getClass().getResource("image.png").getFile());