繁体   English   中英

WebResource分段发布请求

[英]WebResource multipart post request

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

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

这是我的审判:

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

错误显示:

“当前请求不是多部分请求”

下面的代码将文件和ContentDispostion都发送到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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM