WebResource multipart post request

五迷三道 提交于 2020-01-07 03:07:19

问题


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"


回答1:


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


来源:https://stackoverflow.com/questions/35341016/webresource-multipart-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!