2

I have been trying to upload a multipart file in my Java Spring MVC Web Application using a POST REST service method. I have using the following REST service method to upload the file and this works fine when i choose the file using Postman REST service.

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


            }

But when i tried to pass the multipart file as parameter to a POST REST service method in the controller. Its not working fine. So how can i pass multipart file as a queryparam to a POST REST service method.

  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
 }

I have the following bean in my root-context.xml file

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


    </bean>

Any help is appreciated.

K.Sumith
  • 175
  • 1
  • 5
  • 12
  • What do you mean by : `when i tried to pass the multipart file as queryparam to a POST REST service method in the controller`? And how do you do that? – akuma8 Oct 31 '17 at 12:23
  • First, what does your question have to do with REST? Next, why is sending the image as multipart body not an option? Why do you want the data as query-string? Although [URIs](https://tools.ietf.org/html/rfc3986) are not limited to a certain length, most client/browser implementations do [limit the actual length](https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers). Depending on the size of the image a base64 representation of the image-bytes might easily exceed the threshold if added as query-string – Roman Vottner Oct 31 '17 at 12:24
  • I need to know how we can pass multipart file as a parameter to the POST REST service method – K.Sumith Oct 31 '17 at 12:34
  • Do you want to append additional form data to the uploaded file? If so, maybe this [tutorial](http://www.baeldung.com/spring-file-upload) might be helpful – Roman Vottner Oct 31 '17 at 12:47

1 Answers1

1

It is easy and a little bit strange. Do use @PathVariable instead of @RequestParam. I faced with this situation couple month ago. I do not know why it so, but snippet below works in my project.

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

Look at JerseyRestClientMultipartUpload.java to get example how to send MultiPart using Jersey.

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);
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • Actually how can i pass multipart file to REST service method from the controller method. I am getting error while passing file as a parameter shown below WebResource webResource = client.resource(serviceRestDomainName + "/rest/v3/user/upload"); ClientResponse responseMsg = webResource .queryParam("file", file) – K.Sumith Oct 31 '17 at 12:47
  • I do not recognize the framework you use. `queryParam` should set query paramters after `?`. But you have to use another method, that set variable for url before `?`. Yes you do not have smth `{fileUpload}` in you query, but it should work (sorry I did not tried it, I used postman to check it. And it works well now.) – Oleg Cherednik Oct 31 '17 at 12:53