WebResource分段发布请求 [英] WebResource multipart post request

查看:189
本文介绍了WebResource分段发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用java测试将发布请求发送到此rest函数:

I need to send post request using java test to this rest function :

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String handleFileUpload(@PathVariable Long ownerId, @RequestBody MultipartFile file,
        HttpServletResponse response) throws IOException {
//Some function
}


这是我的审判


this is my trial:

File file = new File("filePath");
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE);
WebResource webResource = createResourceClient("restPath", user);
ClientResponse response = webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, fileDataBodyPart);


,错误提示:


and the error says:

当前请求不是多部分请求"

"The current request is not a multipart request"

推荐答案

下面的代码将文件和ContentDispostion都发送到REST API.

The below piece of code will send both the file and the ContentDispostion to the REST API.

FormDataBodyPart formPart = new FormDataBodyPart(FormDataContentDisposition.name("file").fileName("YourFileName").build(),
            inputStream,MediaType.APPLICATION_OCTET_STREAM_TYPE);

    MultiPart multipart = new FormDataMultiPart().bodyPart(formPart);
    resource = resource.path("Your Rest api path");
ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, multipart);

这篇关于WebResource分段发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆