簡體   English   中英

如何將多部分圖像文件作為參數傳遞給POST REST服務

[英]How To Pass Multipart Image File as parameter to a POST REST Service

我一直在嘗試使用POST REST服務方法在Java Spring MVC Web應用程序中上傳多部分文件。 我已使用以下REST服務方法上傳文件,並且當我使用Postman REST服務選擇文件時,此方法工作正常。

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


            }

但是,當我嘗試將multipart文件作為參數傳遞給控制器​​中的POST REST服務方法時。 它不能正常工作。 因此,如何將多部分文件作為queryparam傳遞給POST REST服務方法。

  In my controller class I have:



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

我的root-context.xml文件中有以下bean

<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">


    </bean>

任何幫助表示贊賞。

這很容易,有些奇怪。 不要使用@PathVariable而不是@RequestParam 我在兩個月前面對了這種情況。 我不知道為什么,但是下面的代碼片段在我的項目中起作用。

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/upload-image", consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_VALUE)
    public String uploadProfileImage(@PathVariable("fileUpload") MultipartFile file) {
        // ...
    }

查看JerseyRestClientMultipartUpload.java以獲取示例如何使用Jersey發送MultiPart示例。

final MultiPart multiPart = new FormDataMultiPart()
                .field("description", "Picture of Jabba the Hutt", MediaType.TEXT_PLAIN_TYPE)
                .field("characterProfile", jsonToSend, MediaType.APPLICATION_JSON_TYPE)
                .field("filename", fileToUpload.getName(), MediaType.TEXT_PLAIN_TYPE)
                .bodyPart(fileDataBodyPart);
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);

// POST request final
final WebResource resource = client.resource(API_URI)
ClientResponse response = resource.type("multipart/form-data").post(ClientResponse.class, multiPart);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM