如何将文件作为参数传递给POST REST服务

时间:2017-10-31 13:59:43

标签: java rest spring-mvc

我一直在尝试通过POST REST服务方法在我的Java Spring MVC Web应用程序中上传多部分文件。我正在使用JerseyRestClient。我有控制器方法包含我尝试使用Web服务上传的文件。

我有以下REST服务方法。

@RequestMapping(value="/upload", method=RequestMethod.POST)
     public @ResponseBody String handleFileUpload( @RequestParam("file") MultipartFile file, ModelMap model)
     {

         // codes

        }

控制器方法:

@RequestMapping(value = "/upload-image", method = RequestMethod.POST)
 public String uploadProfileImage(@RequestParam("fileUpload") MultipartFile fileUpload, Model model, HttpServletRequest request, HttpServletResponse response)
 {

    WebResource webResource = client.resource(serviceRestDomainName + "/upload");
         ClientResponse responseMsg = webResource
                .queryParam("file", file)
                .queryParam("userId", String.valueOf(userId))
                .queryParam("siteId", String.valueOf(site.getSiteId()))
                .post(ClientResponse.class);
         returnString = responseMsg.getEntity(String.class);
 }

Passing file as a parameter using Postman rest client  所以我需要知道如何将文件作为POST REST服务的参数传递。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您需要声明

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
     <property name="maxUploadSize" value="268435456"/>
 </bean>

还尝试添加标题

@RequestMapping(value = "/upload-image", headers=("content-type=multipart/*"), method = RequestMethod.POST)